将数据从Azure Blob存储复制到AWS S3

时间:2018-10-25 12:49:20

标签: amazon-s3 ssis azure-storage azure-storage-blobs azure-data-factory-2

我是Azure数据工厂的新手,并且有一个有趣的要求。

我需要将文件从Azure Blob存储移动到Amazon S3,最好使用Azure数据工厂。

但是不支持将S3作为接收器;

enter image description here

https://docs.microsoft.com/en-us/azure/data-factory/copy-activity-overview

我从这里阅读的各种评论中也可以理解,您不能直接从Blob存储复制到S3-您需要在本地下载文件,然后将其上传到S3。

没有人知道在Data Factory,SSIS或Azure Runbook中可以做到这一点的任何示例吗,我想一种选择是编写一个从Data Factory调用的Azure逻辑应用程序或函数。

2 个答案:

答案 0 :(得分:1)

设法做到这一点-对其他人可能有用。

我决定编写一个使用HTTP请求作为触发器的Azure函数。

这两篇文章对我有很大帮助;

How can I use NuGet packages in my Azure Functions?

Copy from Azure Blob to AWS S3 using C#

如果您使用的是Azure函数2.x,请注意我对Nuget软件包的回答。

这里是代码-您可以根据需要修改此基础。 我返回JSON序列化对象,因为Azure数据工厂需要此作为来自管道发送的http请求的响应;

#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"
#r "System.Net.Http"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Net.Http;
using Amazon.S3; 
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Amazon.S3.Util;


public static async  Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("Example Function has recieved a HTTP Request");

    // get Params from query string
    string blobUri = req.Query["blobUri"];
    string bucketName = req.Query["bucketName"];

    // Validate query string
    if (String.IsNullOrEmpty(blobUri) || String.IsNullOrEmpty(bucketName)) {

        Result outcome = new Result("Invalid Parameters Passed to Function",false,"blobUri or bucketName is null or empty");
        return new BadRequestObjectResult(outcome.ConvertResultToJson());
    }

    // cast the blob to its type
    Uri blobAbsoluteUri = new Uri(blobUri);
    CloudBlockBlob blob = new CloudBlockBlob(blobAbsoluteUri);

    // Do the Copy
    bool resultBool = await CopyBlob(blob, bucketName, log);

    if (resultBool) { 
        Result outcome = new Result("Copy Completed",true,"Blob: " + blobUri + " Copied to Bucket: " + bucketName);
        return (ActionResult)new OkObjectResult(outcome.ConvertResultToJson());       
    }
    else {
        Result outcome = new Result("ERROR",false,"Copy was not successful Please review Application Logs");
        return new BadRequestObjectResult(outcome.ConvertResultToJson()); 
    }  
}

static async Task<bool> CopyBlob(CloudBlockBlob blob, string existingBucket, ILogger log) {

        var accessKey = "myAwsKey";
        var secretKey = "myAwsSecret";
        var keyName = blob.Name;

        // Make the client 
        AmazonS3Client myClient = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.EUWest1);

        // Check the Target Bucket Exists; 
        bool bucketExists = await AmazonS3Util.DoesS3BucketExistAsync (myClient,existingBucket);

        if (!bucketExists) {
            log.LogInformation("Bucket: " + existingBucket + " does not exist or is inaccessible to the application");
            return false;
        }

        // Set up the Transfer Utility
        TransferUtility fileTransferUtility = new TransferUtility(myClient);

        // Stream the file
        try {

            log.LogInformation("Starting Copy");

            using (var stream = await blob.OpenReadAsync()) {

                // Note: You need permissions to not be private on the source blob
                log.LogInformation("Streaming");

                await fileTransferUtility.UploadAsync(stream,existingBucket,keyName);

                log.LogInformation("Streaming Done");   
            }

            log.LogInformation("Copy completed");
        }
        catch (AmazonS3Exception e) {
                log.LogInformation("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        catch (Exception e) {
                log.LogInformation("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
                return false;
        }

        return true; 
    }

public class Result {

    public string result;
    public bool outcome;
    public string UTCtime;
    public string details; 

    public Result(string msg, bool outcomeBool, string fullMsg){
        result=msg;
        UTCtime=DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt");
        outcome=outcomeBool;
        details=fullMsg;
    }

    public string ConvertResultToJson() {
        return JsonConvert.SerializeObject(this);
    } 
}

答案 1 :(得分:0)

使用AzCopy从Azure存储中下载文件到临时本地存储库中

您可以将文件从Azure云存储下载到本地系统,只需遵循以下命令,使用递归标志复制所有文件

azcopy /Source:[source_container_url] /Dest:[local_file_path] /Sourcekey:[source_storage_account_access_key] /s

使用aws s3 cp命令将本地文件上传到Amazon S3

 aws s3 cp local_file_path s3://my-bucket/ --recursive