10

次のコードがあります。

public static void UploadStreamToBlob(Stream stream, string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
    blobContainer.CreateIfNotExists();
    blobContainer.SetPermissions(
        new BlobContainerPermissions
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });

    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
    long streamlen = stream.Length;  <-- This shows 203 bytes
    blockBlob.UploadFromStream(stream);        
}

public static Stream DownloadStreamFromBlob(string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

    Stream stream = new MemoryStream();
    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);

    if (blockBlob.Exists())
    {
        blockBlob.DownloadToStream(stream);
        long streamlen = stream.Length;  <-- This shows 0 bytes
        stream.Position = 0;          
    }

    return stream;
}

これを Azure エミュレーターで実行しています。これは、Sql サーバーを指しています。

私が知る限り、UploadFromStream はデータを正しく送信しているように見えますが、DownloadStreamFromBlob を実行しようとすると、長さ 0 のストリームが返されます。blockBlob.Exists は true を返しているので、そこにあると思います。ストリームが空である理由がわかりません。

ところで、両方の呼び出しで containerName と blobName のテストとテストを渡しています。

何か案は?

4

1 に答える 1

15

あ、分かった…

次の行:

long streamlen = stream.Length;
blockBlob.UploadFromStream(stream);   

に変更する必要があります

long streamlen = stream.Length;  
stream.Position = 0;
blockBlob.UploadFromStream(stream);   
于 2013-08-07T22:08:05.917 に答える