加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 05:46:37 所属栏目:Windows 来源:网络整理
导读:我正在尝试将文件上传到 Windows Azure blob存储.根据我的理解,您可以将文件上传到类似于子目录的内容.我想将文件上传到blob,使文件基本上驻留在/TestContainer/subDirectory1/subDirectory2/file.png中 // Setup the Windows Aure blob clientCloudStorageA
我正在尝试将文件上传到 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提到有一种方法可以做到这一点.但是,它没有告诉你如何做到这一点.

谢谢!

解决方法

有几种方法.更简单的方法是使用/ maker作为@ 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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读