Please note that this is my first time doing anything in C# so please be kind, I might have made some very basic mistakes. (and yes I know I shouldnt hardcode keys but will fix it when the code does what I want).
I am trying to create an Azure Function that copies any new items from Blob storage to AWS S3. I have managed to copy from blob to blob using the code from this article: https://cmatskas.com/copy-azure-blob-data-between-storage-accounts-using-functions/
I have tried to amend that code to instead save to an AWS S3 bucket. While this code compiles successfully and gives me successful log entires, it doesn't copy any files. Any ideas?
using System;
using System.IO;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
public async static void Run(CloudBlockBlob myBlob, TraceWriter log)
{
await CopyBlob(myBlob, log);
}
private async static Task CopyBlob(CloudBlockBlob myBlob, TraceWriter log)
{
var existingBucketName = "bucketname";
var keyName = "backup";
var accessKey = "key";
var secretKey = "secretkey";
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(accessKey,secretKey,Amazon.RegionEndpoint.eu-west-2));
log.Info("Starting Copy");
try{
using (var stream = await myBlob.OpenReadAsync())
{
await fileTransferUtility.UploadAsync(stream, existingBucketName, keyName);
}
log.Info("Copy completed");
}
catch(Exception ex){
log.Error(ex.Message);
log.Info("Copy failed");
}
finally{
log.Info("Operation completed");
}
}
Edit: Got it working for anyone finding this in the future.
using System;
using System.IO;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
public async static void Run(CloudBlockBlob myBlob, TraceWriter log)
{
await CopyBlob(myBlob, log);
}
private async static Task CopyBlob(CloudBlockBlob myBlob, TraceWriter log)
{
var existingBucketName = "bucketname";
var keyName = myBlob.Name;
var accessKey = "accesskey";
var secretKey = "secretkey";
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(accessKey,secretKey,Amazon.RegionEndpoint.eu-west-2));
log.Info("Starting Copy");
try{
using (var stream = await myBlob.OpenReadAsync())
{
await fileTransferUtility.UploadAsync(stream,existingBucketName,keyName);
}
log.Info("Copy completed");
}
catch(Exception ex){
log.Error(ex.Message);
log.Info("Copy failed");
}
finally{
log.Info("Operation completed");
}
}