[英]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 Manager
或IAM
来存储和管理您的证书和密钥:
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.