英文:
How to Process file on S3 event through AWS lambda using C#
问题
我正在寻找用于在PUT事件上从S3读取文件并将文件上传到另一个存储桶的C#代码块。我对C#相当新,看到大多数博客都是针对Python和Java编写的。任何帮助将不胜感激。
谢谢,
英文:
I am looking for C# code blocks to read file from S3 on PUT event and upload the file to another bucket. I am fairly new to C# and see most of the blogs are either written for python and java. Any help will be highly appreciated.
Thanks,
答案1
得分: 3
I know it's too late but maybe this will help someone else.
你知道现在可能已经太晚了,但也许这会帮助其他人。
You need to add Amazon.Lambda.S3Events Nuget package.
您需要添加Amazon.Lambda.S3Events Nuget包。
See code, this is how your Lambda Function should look like:
请查看代码,这是您的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;
}
}
You can find AWS Toolkit for Visual Studio here:
您可以在此处找到AWS Toolkit for Visual Studio:
https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-toolkit.html
英文:
I know it's too late but maybe this will help someone else.
You need to add Amazon.Lambda.S3Events Nuget package. See code, this is how your Lambda Function should look like:
<!-- language: csharp -->
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;
}
}
You can find AWS Toolkit for Visual Studio here:
https://docs.aws.amazon.com/lambda/latest/dg/csharp-package-toolkit.html
答案2
得分: 0
- 当新对象被创建时,配置一个 Amazon S3 事件以触发 AWS Lambda 函数
- 创建的对象的详细信息将通过
event
传递给 Lambda 函数 - 然后,您的 Lambda 函数应调用
CopyObject()
来将对象复制到另一个存储桶(无需下载/上传)
参考链接:复制对象 - Amazon 简单存储服务
英文:
The flow would be:
- Configure an Amazon S3 Event to trigger the AWS Lambda function when a new object is created
- Details of the object created will be passed to the Lambda function via the
event
- Your Lambda function should then call
CopyObject()
to duplicate the object to another bucket (no need to download/upload)
答案3
得分: 0
创建一个存储桶并创建具有对您创建的存储桶的访问权限的IAM角色。将该角色分配给您的Lambda函数(无论是通过AWS控制台还是如果您使用Visual Studio则通过AWS SDK。以下屏幕截图中提供了选项)
在您发布后,转到“事件触发器”选项卡并添加您创建的存储桶。确保设置此触发器后再次上传代码。
上传一个文件并检查云监视日志,以查看函数是否已执行。这里附有一些示例代码。
英文:
Create a bucket and create IAM role that has access to the bucket you created. Assign to that role to your lambda function ( either thru AWS console or AWS SDK if you use Visual studio. the option is available in the following screenshot1)
After you published go to the Event Trigger tab and add the bucket you created. Make sure you upload the code again once you set this trigger
Upload a file and check from the cloud watch log to see function has executed. Some sample code is attached here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论