我正在尝试将文件上传到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提到有一种方法可以做到这一点。但是,它没有告诉你如何做到这一点。
谢谢!
答案 0 :(得分:12)
有两种方法。更简单的方法是使用/
字符作为@ 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);
}
答案 1 :(得分:1)
我相信您所要做的就是将子目录包含在blob名称中,如/my/sub/directory/file.txt
目录不存在于容器名称之外,文件名中由/
字符分隔的字符串。