Skip to content

Instantly share code, notes, and snippets.

@comitieutu
Created July 22, 2020 08:27
Show Gist options
  • Save comitieutu/998edf43f566b81567d4144a8b95e106 to your computer and use it in GitHub Desktop.
Save comitieutu/998edf43f566b81567d4144a8b95e106 to your computer and use it in GitHub Desktop.
Copy storage blob from http
using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System.Net;
using System.Linq;
namespace azurefunction
{
public class StorageBlobFunction
{
/// <summary>
/// The delay task if pending
/// </summary>
private const int DELAY_OF_TASK = 1000;
/// <summary>
/// Download blob image from storage
/// </summary>
/// <param name="myTimer">The TimerInfo.</param>
/// <param name="log">The log.</param>
/// <returns></returns>
[FunctionName("storage-blob-copy")]
public async Task DownloadBlobAsync(
[TimerTrigger("0 00 22 * * *")] TimerInfo myTimer, ILogger log)
{
//destination container
string destConnectionString = "DestConnectionString";
string destContainerName = "DestContainerName";
// create the blob client for the destination storage account
CloudStorageAccount destStorageAccount = CloudStorageAccount.Parse(destConnectionString);
CloudBlobClient destClient = destStorageAccount.CreateCloudBlobClient();
var destContainer = destClient.GetContainerReference(destContainerName);
await destContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
var blobUrls = new string[]
{
"http://sourceContainer/blob1.jpg",
"http://sourceContainer/blob2.png"
};
try
{
foreach (var url in blobUrls)
{
var contentMd5 = "";
using (WebClient client = new WebClient())
{
client.OpenRead(url);
contentMd5 = client.ResponseHeaders.Get("Content-MD5"); // use to check if blob modified
}
var fileName = url.Split("/").Last();
// copy to the blob
CloudBlob destBlob = destContainer.GetBlobReference(fileName);
await destBlob.StartCopyAsync(new Uri(url)).ConfigureAwait(false);
while (destBlob.CopyState.Status == CopyStatus.Pending)
{
await Task.Delay(DELAY_OF_TASK).ConfigureAwait(false);
await destBlob.FetchAttributesAsync().ConfigureAwait(false);
destBlob = destContainer.GetBlockBlobReference(destBlob.Name);
}
if (destBlob.CopyState.Status == CopyStatus.Success)
{
//do something
}
}
}
catch (StorageException e)
{
log.LogError($"HTTP error code {e.RequestInformation.HttpStatusCode} : {e.RequestInformation.ErrorCode}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment