使用C#从Azure Blob复制到AWS S3 [英] Copy from Azure Blob to AWS S3 using C#

查看:160
本文介绍了使用C#从Azure Blob复制到AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这是我第一次用C#做任何事情,所以请客气,我可能犯了一些非常基本的错误. (是的,我知道我不应该对硬键进行硬编码,但是会在代码执行我想要的操作时将其修复).

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).

我正在尝试创建一个Azure功能,该功能将所有新项目从Blob存储复制到AWS S3.我已成功使用本文中的代码从一个Blob复制到另一个Blob: https://cmatskas.com/copy-azure-blob-data-between-storage-accounts-using-functions/

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/

我试图修改该代码以代替保存到AWS S3存储桶中.尽管此代码可以成功编译并为我提供成功的日志完整记录,但它不会复制任何文件.有什么想法吗?

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

以后所有发现它的人都可以使用.

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

推荐答案

您应该会看到关于此的警告,但是您的void方法很可能会在此处引起问题.

You should be seeing a warning about this, but your void method is likely causing the issue here.

请将您的功能代码更新为以下内容:

Please update your function code to the following:

public async static Task Run(CloudBlockBlob myBlob, TraceWriter log) 
{
    await CopyBlob(myBlob, log);
}

请注意从void更改为Task

这篇关于使用C#从Azure Blob复制到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆