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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
# Storage
This document explains how to get started using Azure Storage Service with Fog. With this gem you can create, update, list or delete storage accounts.
## Usage
First of all, you need to require the Fog library by executing:
```ruby
require 'fog/azurerm'
```
## Create Connection
If you only want to manage the storage data, you can create the connection without the Azure subscription information:
```ruby
fog_storage_service = Fog::AzureRM::Storage.new(
azure_storage_account_name: '<Storage Account Name>', # Name of an Azure Storage Account
azure_storage_access_key: '<Storage Account Key>', # Key of an Azure Storage Account
environment: '<AzureCloud/AzureChinaCloud/AzureUSGovernment/AzureGermanCloud>' # Azure cloud environment. Default is AzureCloud.
)
```
## Check Storage Container Existence
```ruby
fog_storage_service.directories.check_container_exists('<Container Name>')
```
## Create a storage container
Create a storage container in the current storage account.
```ruby
directory = fog_storage_service.directories.create(
key: '<Container Name>',
public: <True/False>
)
puts directory.key
```
## List storage containers
List all the storage containers in the current storage accounts.
```ruby
fog_storage_service.directories.each do |directory|
puts directory.key
end
```
## Get the access control level of the storage container
Get the permissions for the specified container. The permissions indicate whether container data may be accessed publicly.
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
puts directory.acl
```
## Set the access control level of the storage container
Set the permissions for the specified container. The permissions indicate whether container data may be accessed publicly. The container permissions provide the following options for managing container access:
- ###### Container
Full public read access. Container and blob data can be read via anonymous request. Clients can enumerate blobs within the container via anonymous request, but cannot enumerate containers within the storage account.
- ###### Blob
Public read access for blobs only. Blob data within this container can be read via anonymous request, but container data is not available. Clients cannot enumerate blobs within the container via anonymous request.
- ###### Nil
No public read access. Container and blob data can be read by the account owner only.
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
directory.acl = '<Container Name>'
directory.save(is_create: <True/False>)
```
## Delete the storage container
Mark the specified container for deletion. The container and any blobs contained within it are later deleted during garbage collection.
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
puts directory.destroy
```
## Upload data as a block blob
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
options = {
key: '<Blob Name>',
body: '<Blob Content>'
}
new_block_blob = directory.files.create(options)
puts new_block_blob.inspect
```
## Upload a local file as a block blob
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
File.open('<File Path>') do |file|
options = {
key: '<Blob Name>',
body: file
}
new_block_blob = directory.files.create(options)
puts new_block_blob.inspect
end
```
## Upload VHD data as a page blob
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
options = {
key: '<Blob Name>',
body: '<Blob Content>',
blob_type: '<Blob Type>'
}
new_page_blob = directory.files.create(options)
puts new_page_blob.inspect
```
## Upload a local VHD as a page blob
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
File.open('<File Path>') do |file|
options = {
key: '<Blob Name>',
body: file,
blob_type: '<Blob Type>'
}
new_page_blob = directory.files.create(options)
puts new_page_blob.inspect
end
```
## Copy Blob from one container to another
```ruby
directory = fog_storage_service.directories.get('<Source Container Name>', max_keys: <Maximum No. of Keys Value>)
copied_blob = directory.files.head('<Source Blob Name>').copy('<Destination Container Name>', '<Destination Blob Name>')
puts copied_blob.inspect
```
## Copy Blob from one uri to self
```ruby
directory = fog_storage_service.directories.get('<Destination Container Name>', max_keys: <Maximum No. of Keys Value>)
copied_blob = directory.files.new(key: '<Destination Blob Name>')
copied_blob.copy_from_uri('<Source Blob Uri>')
puts copied_blob.inspect
```
## Download a small blob to a local file
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
blob = directory.files.get('<Blob Name>')
File.open('<File Path>', 'wb') do |file|
file.write(blob.body)
end
puts "File Size: #{::File.size <File Path>}"
```
## Download a large blob to a local file
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
File.open('<File Path>', 'wb') do |file|
directory.files.get('<Blob Name>') do |chunk, remaining_bytes, total_bytes|
puts "remaining_bytes: #{remaining_bytes}, total_bytes: #{total_bytes}"
file.write(chunk)
end
end
puts "File Size: #{::File.size <File Path>}"
```
## Delete the storage blob
Mark the specified blob for deletion. The blob is later deleted during garbage collection.
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
blob = directory.files.head('<Blob Name>')
puts blob.destroy
```
## Set storage blob properties
Set the storage blob properties.
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
blob = directory.files.head('<Blob Name>')
blob.content_language = '<Language>'
blob.content_disposition = '<Content Disposition Type>'
blob.save(update_body: <True/False>)
```
## Metadata
Metadata allows us to provide descriptive information about specific containers or blobs. This is simply providing name/value pairs of data we want to set on the container or blob.
### Get Blob Metadata
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
blob = directory.files.head('<Blob Name>')
puts blob.metadata
```
### Set Blob Metadata
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
blob = directory.files.head('<Blob Name>')
blob.metadata = {
Category: '<Category Value>',
Resolution: '<Resolution Value>'
}
blob.save(update_body: <True/False>)
```
### Get Container Metadata
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
puts directory.metadata
```
### Set Container Metadata
```ruby
directory = fog_storage_service.directories.get('<Container Name>', max_keys: <Maximum No. of Keys Value>)
directory.metadata = {
CreatedBy: '<Username>',
SourceMachine: '<Machine Name>',
category: '<Category Value>',
docType: '<Document Type>'
}
directory.save(is_create: <True/False>)
```
## Support and Feedback
Your feedback is appreciated! If you have specific issues with the fog ARM, you should file an issue via Github.
|