【Azure Blob】關閉Blob 匿名訪問,iOS Objective-C SDK連接Storage Account報錯
問題描述
iOS Objective-C 應用,連接Azure Storage Account, 根據官網Example代碼,在沒有關閉Storage Account的匿名訪問時,程序正常運行。

但是,只要關閉了匿名訪問,上傳blob到Container中,就會報錯:Public access is not permitted on this storage account

問題解答
查看示例代碼:
-(void)createContainerWithPublicAccess{ NSError *accountCreationError; // Create a storage account object from a connection string. AZSCloudStorageAccount *account = [AZSCloudStorageAccount accountFromConnectionString:@"DefaultEndpointsProtocol=https;AccountName=your_account_name_here;AccountKey=your_account_key_here" error:&accountCreationError]; if(accountCreationError){ NSLog(@"Error in creating account."); } // Create a blob service client object. AZSCloudBlobClient *blobClient = [account getBlobClient]; // Create a local container object. AZSCloudBlobContainer *blobContainer = [blobClient containerReferenceFromName:@"containerpublic"]; // Create container in your Storage account if the container doesn't already exist [blobContainer createContainerIfNotExistsWithAccessType:AZSContainerPublicAccessTypeContainer requestOptions:nil operationContext:nil completionHandler:^(NSError *error, BOOL exists){ if (error){ NSLog(@"Error in creating container."); } }]; }
關鍵就是 blobContainer createContainerIfNotExistsWithAccessType:AZSContainerPublicAccessTypeContainer 這一句代碼。因為示例代碼中使用的是Public Access方式create container,雖然連接字符串中由Account Key,但是代碼中使用的是Public Access。所以當關閉Public Access后,程序就會報錯。
把 AZSContainerPublicAccessTypeContainer 修改為 AZSContainerPublicAccessTypeOff 就可以了。

NOTES:
- No public read access: The container and its blobs can be accessed only with an authorized request. This option is the default for all new containers.
- Public read access for container and its blobs: Container and blob data can be read by anonymous request, except for container permission settings and container metadata. Clients can enumerate blobs within the container by anonymous request, but cannot enumerate containers within the storage account.
- Public read access for blobs only: Blobs within the container can be read by anonymous request, but container data is not available anonymously. Anonymous clients cannot enumerate the blobs within the container.
參考資料
設置Storage Account Container容器權限 : https://learn.microsoft.com/zh-cn/previous-versions/azure/storage/blobs/storage-ios-how-to-use-blob-storage#set-container-permissions
[END]
當在復雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 云中,恰是如此!

浙公網安備 33010602011771號