且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-08 23:27:41

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

It's actually pretty simple and you can ask Azure Storage to do the work for you :).

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

Essentially what you have to do is invoke Copy Blob operation. With this operation, you can specify any publicly accessible URL and Azure Storage Service will create a blob for you in Azure Storage by copying the contents of that URL.

        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");