azure-storage - 使用 Windows Azure Blob 存储中的子目录

标签 azure-storage azure-blob-storage

我正在尝试将文件上传到 Windows Azure blob 存储。根据我的理解,您可以将文件上传到类似于子目录的内容。我想将文件上传到 blob,以便该文件基本上位于 /TestContainer/subDirectory1/subDirectory2/file.png

// Setup the Windows Aure blob client
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("BlobStorage");
CloudBlobClient client = storageAccount.CreateCloudBlobClient();

// Retrieve the TestContainer container from blob storage
CloudBlobContainer container = client.GetContainerReference("TestContainer");
if (container.CreateIfNotExist())
  container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

// Setup the blob
CloudBlob blob = container.GetBlobReference("");

// Create the meta data for the blob
NameValueCollection metadata = new NameValueCollection();
metadata["id"] = fileID.ToString();
blob.Metadata.Add(metadata);

// Store the blob 
byte[] bytes = GetFileBytes();
blob.UploadByteArray(bytes);

如何上传具有目录结构的文件?链接 here提到有一种方法可以做到。但是,它没有向您展示如何操作。

谢谢!

最佳答案

有几种方法。更简单的方法是使用 /已经提到@makerofthings7 的字符。您也可以使用 CloudBlobDirectory如果您愿意,可以反对。这是一个显示两者的示例。

CloudBlobContainer testContainer = blobClient.GetContainerReference("testcontainer");

//Upload using a CloudBlobDirectory object
var dir = testContainer.GetDirectoryReference("UsingCloudDirectory/foo/bar/baz/");
var blobRef = dir.GetBlockBlobReference("BlobByDir.bin");

using (MemoryStream ms = new MemoryStream(new byte[] { 0x0 }))
{
    blobRef.UploadFromStream(ms);
}

//Upload using the filename without a CloudBlobDirectory
var blobRef2 = testContainer.GetBlockBlobReference("UsingBlobName/foo/bar/baz/BlobByName.bin");
using (MemoryStream ms = new MemoryStream(new byte[] { 0x0 }))
{
    blobRef2.UploadFromStream(ms);
}

关于azure-storage - 使用 Windows Azure Blob 存储中的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328493/

相关文章:

c# - 从 Blob Azure 下载重定向错误

windows - 如何使用 windows azure powershell 获取系统时间

azure - 读取存储为 .h5 的 blob 并在 Python 中的 Azure Functions 中加载 keras 模型

Azure博客功能触发容器错误

azure - 如何使用 PUT 和 SaS Url 将大文件 (~10GB) 直接从浏览器上传到 Azure Blob 存储?

azure - 在 Azure 存储中使用 CORS 来防止文件被非法访问

azure - azure python SDK 中是否有与 powershell AzStorageAccount 等效的类或模块?

azure - 用于操作 Azure blob 容器的共享访问签名

python - 如何添加 Blob 存储绑定(bind)?

azure-functions - 带有事件中心触发器的 Azure 函数写入重复的消息