如何从网站图像上传文件到Azure Blob存储?

时间:2017-04-24 16:17:02

标签: azure blob azure-storage azure-storage-blobs blobstorage

如何从www.site.com/amazing.jpg将文件上传到Azure Blob存储? 从网址多个上传文件到Azure Blob存储。 我找不到这种方式。我试过很多不成功的方法:( 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

实际上非常简单,您可以要求Azure Storage为您完成工作:)。

基本上你要做的就是调用Copy Blob操作。通过此操作,您可以指定任何可公开访问的URL,Azure存储服务将通过复制该URL的内容在Azure存储中为您创建Blob。

        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudBlobClient();
        var container = client.GetContainerReference("temp");
        var blob = container.GetBlockBlobReference("amazing.jpg");
        blob.StartCopy(new Uri("www.site.com/amazing.jpg"));
        //Since copy is async operation, if you want to see if the blob is copied successfully, you must check the status of copy operation
        do
        {
            System.Threading.Thread.Sleep(1000);
            blob.FetchAttributes();
            var copyStatus = blob.CopyState.Status;
            if (copyStatus != CopyStatus.Pending)
            {
                break;
            }
        } while (true);
        Console.WriteLine("Copy operation finished");