我正在尝试将文件上传到 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/