5

ファイルをWindowsAzureBLOBストレージにアップロードしようとしています。私の理解では、サブディレクトリに似たものにファイルをアップロードできます。ファイルが基本的にに存在するように、ファイルを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);

ディレクトリ構造のファイルをアップロードするにはどうすればよいですか?ここのリンクはそれをする方法があると述べています。ただし、その方法は示されていません。

ありがとうございました!

4

2 に答える 2

16

いくつかの方法があります。簡単な方法は/、@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);
}
于 2013-01-15T15:46:04.420 に答える
1

I believe all you have to do is include the subdirectories in the blob name like /my/sub/directory/file.txt

Directories don't really exist outside of the container name and the strings delimited by a / character in the file name.

于 2013-01-15T03:42:30.283 に答える