forked from oracle/oci-powershell-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams_ObjectStorage.ps1
More file actions
89 lines (73 loc) · 3.96 KB
/
Copy pathStreams_ObjectStorage.ps1
File metadata and controls
89 lines (73 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
This examples demonstrates how to work with Stream inputs/ouputs using some features introduced by OCI PS Modules.
This example requires:
1) Module OCI.PSModules.Objectstorage. Install the module from Powershell Gallery.
2) Setting up the environment variable CompartmentId to a Compartment OCID.
#>
$UserErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrEmpty($env:CompartmentId)) {
Throw 'Configure $env:CompartmentId in the PS Session'
}
try {
#Import the module
Import-Module OCI.PSModules.Objectstorage
#Setup
$RandomSufix = Get-Random -Minimum 1 -Maximum 1000
$DisplayName = "PSExample" + $RandomSufix
$BucketName = $DisplayName -join "Bucket"
$ObjectName = $DisplayName -Join "Object"
$FileName = $DisplayName + ".txt"
$OutputFileName = "Out" + $FileName
#Read CompartmentId environment variable
$CompartmentId = $env:CompartmentId
#Get the namespace of the account
Write-Host "Get-OCIObjectStorageNamespace -CompartmentId $CompartmentId"
$NamespaceName = Get-OCIObjectStorageNamespace -CompartmentId $CompartmentId
$NamespaceName
#Create bucket details
$BucketDetails = New-Object System.Management.Automation.PSObject
$BucketDetails | Add-Member -Name “CompartmentId” -Value $CompartmentId -MemberType NoteProperty
$BucketDetails | Add-Member -Name “Name” -Value $BucketName -MemberType NoteProperty
#Create a new object store bucket
Write-Host "New-OCIObjectStorageBucket -NamespaceName $NamespaceName -CreateBucketDetails $BucketDetails"
$Bucket = New-OCIObjectStorageBucket -NamespaceName $NamespaceName -CreateBucketDetails $BucketDetails
#Read the last response from history
$OCICmdletHistory.LastResponse.Bucket
#Create a text file
"Great to see you here! We are here to help you get started on your cloud(-nine) journey." > $FileName
#Put an object into the created bucket
Write-Host "Write-OCIObjectStorageObject -NamespaceName $NamespaceName -BucketName $Bucket.Name -ObjectName $ObjectName -PutObjectBodyFromFile $FileName -FullResponse"
Write-OCIObjectStorageObject -NamespaceName $NamespaceName -BucketName $Bucket.Name -ObjectName $ObjectName -PutObjectBodyFromFile $FileName -FullResponse | Out-Host
#Read the object back from storage as a Stream
Write-Host "Get-OCIObjectStorageObject -NamespaceName $NamespaceName -ObjectName $ObjectName -BucketName $BucketName"
$Stream = Get-OCIObjectStorageObject -NamespaceName $NamespaceName -ObjectName $ObjectName -BucketName $BucketName
$Stream
#Read the object back from storage and write it to a file
Write-Host "Get-OCIObjectStorageObject -NamespaceName $NamespaceName -ObjectName $ObjectName -BucketName $BucketName -OutputFile $OutputFileName"
Get-OCIObjectStorageObject -NamespaceName $NamespaceName -ObjectName $ObjectName -BucketName $BucketName -OutputFile $OutputFileName
Write-Host "File read from Object Store:"
Cat $OutputFileName
}
finally {
#To Maximize possible clean ups continue on error
$ErrorActionPreference = "Continue"
if ($null -ne $Stream) {
#Remove the created object asking explicit user confirmation
Write-Host "Remove-OCIObjectStorageObject -NamespaceName $NamespaceName -BucketName $BucketName -ObjectName $ObjectName"
Remove-OCIObjectStorageObject -NamespaceName $NamespaceName -BucketName $BucketName -ObjectName $ObjectName
}
if ($null -ne $Bucket) {
#Remove the created bucket asking explicit user confirmation
Write-Host "Remove-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $BucketName -FullResponse"
Remove-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $BucketName -FullResponse
}
#CleanUp
if (Test-Path -Path $FileName) {
rm $FileName
}
if (Test-Path -Path $OutputFileName) {
rm $OutputFileName
}
$ErrorActionPreference = $UserErrorActionPreference
}