简体   繁体   English

如何从Lambda函数访问S3存储桶中的文件

[英]How to access file in S3 bucket from lambda function

I have a file a my S3 bucket and I want to access this file from a Lambda function. 我的S3存储桶中有一个文件,我想从Lambda函数访问此文件。

When I pass the path of this file to one of the methods, I get the error: 当我将此文件的路径传递给方法之一时,出现错误:

Could not find a part of the path '/var/task/https:/s3.amazonaws.com/TestBucket/testuser/AWS_sFTP_Key.pem". 找不到路径'/var/task/https:/s3.amazonaws.com/TestBucket/testuser/AWS_sFTP_Key.pem”的一部分。

For example: 例如:

TestMethod("https://s3.amazonaws.com/TestBucket/testuser/AWS_sFTP_Key.pem")

code: 码:

public void FunctionHandler(S3Event s3Event, ILambdaContext lambdaContext)
        {
            ConnectionInfo connectionInfo = new ConnectionInfo("xxx.xxx.xx.xxx", "testuser",
                                                   new AuthenticationMethod[]{
            new PrivateKeyAuthenticationMethod("testuser", new PrivateKeyFile[] {
                new PrivateKeyFile("https://s3.amazonaws.com/TestBucket/testuser/AWS_sFTP_Key.pem")})
});

            SftpClient sftpClient = new SftpClient(connectionInfo);
            sftpClient.Connect();
            lambdaContext.Logger.Log(sftpClient.WorkingDirectory);
            sftpClient.Disconnect();
        }

You can use AWS SDK for reading the file from S3 as shown below, however I would suggest to use AWS Certificate Manager or IAM for storing and managing your certificates and keys: 您可以使用AWS SDK如下所示从S3读取文件,但是我建议使用AWS Certificate ManagerIAM来存储和管理您的证书和密钥:

PS: Make sure you assign the proper role for your lambda function or bucket policy for your bucket to be able to GetObject from S3 : PS:确保为lambda函数或存储桶策略分配了适当的角色,以便存储桶能够从S3获得GetObject

    RegionEndpoint bucketRegion = RegionEndpoint.USWest2;//region where you store your file

    client = new AmazonS3Client(bucketRegion);

    GetObjectRequest request = new GetObjectRequest();

    request.WithBucketName(BUCKET_NAME);//TestBucket
    request.WithKey(S3_KEY);//testuser/AWS_sFTP_Key.pem

    GetObjectResponse response = client.GetObject(request);

    StreamReader reader = new StreamReader(response.ResponseStream);

    String content = reader.ReadToEnd();

More Help: 更多帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda(C#)是否允许从s3存储桶动态加载DLL并将其实例化以在Lambda函数中使用? - Does AWS Lambda (C#) allow loading DLLs dynamically from a s3 bucket and instantiate it for use within a Lambda function? S3存储桶中的访问被拒绝 - Access Denied in S3 Bucket 如何通过简单URL访问Amazon S3专用存储桶中的文件? - How do I access a file in Amazon S3 private bucket through simple URL? 使用c#将大型文件从一个S3存储桶复制到另一个S3存储桶 - Copy Large file from one S3 bucket to another S3 Bucket using c# 无法在 Dot NET Lambda 函数项目中调整上传到 s3 存储桶的图像的大小 - Unable to resize the image uploaded to s3 bucket in Dot NET Lambda function project 从Web服务器上传文件到S3存储桶 - Upload file to S3 bucket from web server 从AWS S3存储桶读取Excel文件 - Read excel file from AWS S3 Bucket 如何在进度栏中显示文件上传进度-Amazon S3存储桶 - How to display file upload progress in progressbar - Amazon S3 bucket Amazon S3 SELECT从S3存储桶中的.csv文件返回垃圾数据(使用.NET SDK) - Amazon S3 SELECT returning garbage data from a .csv file in S3 Bucket (using .NET SDK) 如何从Amazon S3存储桶获取最早添加的对象? - How to get the oldest added object from Amazon S3 Bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM