[英]AWS S3 upload without access and secret key in Java
I want to upload a file to S3 without using my access and secret key from AWS
server. 我想在不使用
AWS
服务器的访问权限和密钥的情况下将文件上传到S3。 AWS keys should be taken as default. AWS键应作为默认值。 However running the below command in server I can access it without providing any access and secret keys.
但是,在服务器中运行以下命令,我可以访问它而无需提供任何访问和密钥。
aws s3 cp somefile.txt s3://somebucket/
aws s3 cp somefile.txt s3:// somebucket /
From java code its not accessible since it was unable to load credentials. 从Java代码,它无法访问,因为它无法加载凭据。 Below is my code.
以下是我的代码。
AmazonS3 s3client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
You can use the below Java code to get the s3client
instance when you are trying to connect to S3 bucket from EC2 instance. 当您尝试从EC2实例连接到S3存储桶时,可以使用以下Java代码获取
s3client
实例。
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new InstanceProfileCredentialsProvider(false))
.build();
This is the recommended way as the application doesn't require to maintain the access keys in property files. 这是推荐的方法,因为应用程序不需要在属性文件中维护访问键。
Sample policy for IAM role:- IAM角色的示例政策: -
{
"Action": ["s3:PutObject",
"s3:ListBucket",
"s3:GetObject",
"s3:DeleteObject"],
"Resource": ["arn:aws:s3:::yourBucketName",
"arn:aws:s3:::yourBucketName/*"],
"Effect": "Allow",
"Sid": "AllowBucketLinux"
}
As per documentation AWS credentials provider chain that looks for credentials in this order : 根据文档AWS凭证提供程序链,按此顺序查找凭据:
Check you have specify valid credentials in any of above. 检查您是否在上述任何一项中指定了有效凭据。
Ref : http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html 参考: http : //docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.