问题描述
请注意,这是我第一次在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!