How can upload files to Azure Blob Storage from we

2019-09-21 15:27发布

问题:

How can upload file to Azure Blob Storage from www.site.com/amazing.jpg ? Multiple upload files to Azure Blob Storage from urls. I cant find this way. I tried many way unsuccesful :( thank you for help

回答1:

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

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