[英]How to Process file on S3 event through AWS lambda using C#
我正在尋找 C# 代碼塊以在 PUT 事件上從 S3 讀取文件並將文件上傳到另一個存儲桶。 我是 C# 的新手,看到大多數博客都是為 python 和 java 編寫的。 任何幫助將不勝感激。
謝謝,
我知道為時已晚,但也許這會對其他人有所幫助。 您需要添加 Amazon.Lambda.S3Events Nuget 包。 查看代碼,這就是您的 Lambda 函數的樣子:
using System;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.S3Events;
using Amazon.S3;
using Amazon.S3.Model;
public async Task<string> FunctionHandler(S3Event evnt, ILambdaContext context)
{
var s3Event = evnt.Records?[0].S3;
if(s3Event == null)
{
return null;
}
try
{
var destinationBucketName = "your_bucket_to_upload";
var destinationKey= "folder/subfolder/fileName.txt";
var s3 = new AmazonS3Client();
var request = new CopyObjectRequest
{
SourceBucket = s3Event.Bucket.Name,
SourceKey = s3Event.Object.Key,
DestinationBucket = destinationBucketName,
DestinationKey = destinationKey
};
var response = await s3.CopyObjectAsync(request);
return response.HttpStatusCode.ToString();
}
catch(Exception e)
{
context.Logger.LogLine($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
context.Logger.LogLine(e.Message);
context.Logger.LogLine(e.StackTrace);
throw;
}
}
您可以在此處找到適用於 Visual Studio 的 AWS 工具包: https ://docs.aws.amazon.com/lambda/latest/dg/csharp-package-toolkit.html
流程是:
event
傳遞給 Lambda 函數CopyObject()
將對象復制到另一個存儲桶(無需下載/上傳)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.