Is it possible to upload a txt/pdf/png file to Amazon S3 in a single action, and get the uploaded file url as response. If so, is AWS Java sdk the right library that i need to add in my java struts2 webapplication.
是否可以通过单个操作将txt / pdf / png文件上传到Amazon S3,并将上传的文件URL作为响应。如果是这样,AWS Java sdk是否需要在我的java struts2 webapplication中添加正确的库。
Please suggest me a solution for this.
请为我建议一个解决方案。
6 个解决方案
#1
0
To make the file public before uploading you can use the #withCannedAcl
method of PutObjectRequest
:
要在上传之前公开文件,可以使用PutObjectRequest的#withCannedAcl方法:
myAmazonS3Client.putObject(new PutObjectRequest('some-grails-bucket', 'somePath/someKey.jpg', new File('/Users/ben/Desktop/photo.jpg')).withCannedAcl(CannedAccessControlList.PublicRead))
#2
69
No you cannot get the URL in single action but two :)
不,你不能在单个动作中获取URL但是两个:)
First of all, you may have to make the file public before uploading because it makes no sense to get the URL that no one can access. You can do so by setting ACL as Michael Astreiko suggested. You can get the resource URL either by calling getResourceUrl
or getUrl
.
首先,您可能必须在上传之前公开文件,因为获取无人可访问的URL是没有意义的。您可以通过设置ACL作为Michael Astreiko建议来实现。您可以通过调用getResourceUrl或getUrl来获取资源URL。
AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder.defaultClient();
s3Client.putObject(new PutObjectRequest("your-bucket", "some-path/some-key.jpg", new File("somePath/someKey.jpg")).withCannedAcl(CannedAccessControlList.PublicRead))
s3Client.getResourceUrl("your-bucket", "some-path/some-key.jpg");
Note1: The different between getResourceUrl
and getUrl
is that getResourceUrl will return null when exception occurs.
注意1:getResourceUrl和getUrl之间的不同之处在于,当发生异常时,getResourceUrl将返回null。
Note2: getUrl
method is not defined in the AmazonS3 interface. You have to cast the object to AmazonS3Client if you use the standard builder.
注2:AmazonS3界面中未定义getUrl方法。如果使用标准构建器,则必须将对象强制转换为AmazonS3Client。
#3
68
You can work it out for yourself given the bucket and the file name you specify in the upload request.
鉴于您在上传请求中指定的存储桶和文件名,您可以自行解决。
e.g. if your bucket is mybucket
and your file is named myfilename
:
例如如果您的存储桶是mybucket并且您的文件名为myfilename:
https://mybucket.s3.amazonaws.com/myfilename
The s3
bit will be different depending on which region your bucket is in. For example, I use the south-east asia region so my urls are like:
s3位将根据您的桶所在的区域而有所不同。例如,我使用东南亚地区,所以我的网址如下:
https://mybucket.s3-ap-southeast-1.amazonaws.com/myfilename
#4
1
a bit old but still for anyone stumbling upon this in the future:
有点旧,但仍然有人在未来绊倒这个:
you can do it with one line assuming you already wrote the CredentialProvider and the AmazonS3Client.
假设您已经编写了CredentialProvider和AmazonS3Client,您可以使用一行来完成。
it will look like this:
它看起来像这样:
String ImageURL = String.valueOf(s3.getUrl(
ConstantsAWS3.BUCKET_NAME, //The S3 Bucket To Upload To
file.getName())); //The key for the uploaded object
and if you didn't wrote the CredentialProvider and the AmazonS3Client then just add them before getting the URL like this:
如果您没有编写CredentialProvider和AmazonS3Client,那么只需在获取URL之前添加它们:
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"POOL_ID", // Identity pool ID
Regions.US_EAST_1 // Region
);
#5
0
System.out.println("Link : " + s3Object.getObjectContent().getHttpRequest().getURI());
with this you can retrieve the link of already uploaded file to S3 bucket.
使用此功能,您可以检索已上载文件到S3存储桶的链接。
#6
0
Similarly if you want link through s3Client you can use below.
同样,如果您想通过s3Client链接,可以在下面使用。
System.out.println("filelink: " + s3Client.getUrl("your_bucket_name", "your_file_key"));
#1
0
To make the file public before uploading you can use the #withCannedAcl
method of PutObjectRequest
:
要在上传之前公开文件,可以使用PutObjectRequest的#withCannedAcl方法:
myAmazonS3Client.putObject(new PutObjectRequest('some-grails-bucket', 'somePath/someKey.jpg', new File('/Users/ben/Desktop/photo.jpg')).withCannedAcl(CannedAccessControlList.PublicRead))
#2
69
No you cannot get the URL in single action but two :)
不,你不能在单个动作中获取URL但是两个:)
First of all, you may have to make the file public before uploading because it makes no sense to get the URL that no one can access. You can do so by setting ACL as Michael Astreiko suggested. You can get the resource URL either by calling getResourceUrl
or getUrl
.
首先,您可能必须在上传之前公开文件,因为获取无人可访问的URL是没有意义的。您可以通过设置ACL作为Michael Astreiko建议来实现。您可以通过调用getResourceUrl或getUrl来获取资源URL。
AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder.defaultClient();
s3Client.putObject(new PutObjectRequest("your-bucket", "some-path/some-key.jpg", new File("somePath/someKey.jpg")).withCannedAcl(CannedAccessControlList.PublicRead))
s3Client.getResourceUrl("your-bucket", "some-path/some-key.jpg");
Note1: The different between getResourceUrl
and getUrl
is that getResourceUrl will return null when exception occurs.
注意1:getResourceUrl和getUrl之间的不同之处在于,当发生异常时,getResourceUrl将返回null。
Note2: getUrl
method is not defined in the AmazonS3 interface. You have to cast the object to AmazonS3Client if you use the standard builder.
注2:AmazonS3界面中未定义getUrl方法。如果使用标准构建器,则必须将对象强制转换为AmazonS3Client。
#3
68
You can work it out for yourself given the bucket and the file name you specify in the upload request.
鉴于您在上传请求中指定的存储桶和文件名,您可以自行解决。
e.g. if your bucket is mybucket
and your file is named myfilename
:
例如如果您的存储桶是mybucket并且您的文件名为myfilename:
https://mybucket.s3.amazonaws.com/myfilename
The s3
bit will be different depending on which region your bucket is in. For example, I use the south-east asia region so my urls are like:
s3位将根据您的桶所在的区域而有所不同。例如,我使用东南亚地区,所以我的网址如下:
https://mybucket.s3-ap-southeast-1.amazonaws.com/myfilename
#4
1
a bit old but still for anyone stumbling upon this in the future:
有点旧,但仍然有人在未来绊倒这个:
you can do it with one line assuming you already wrote the CredentialProvider and the AmazonS3Client.
假设您已经编写了CredentialProvider和AmazonS3Client,您可以使用一行来完成。
it will look like this:
它看起来像这样:
String ImageURL = String.valueOf(s3.getUrl(
ConstantsAWS3.BUCKET_NAME, //The S3 Bucket To Upload To
file.getName())); //The key for the uploaded object
and if you didn't wrote the CredentialProvider and the AmazonS3Client then just add them before getting the URL like this:
如果您没有编写CredentialProvider和AmazonS3Client,那么只需在获取URL之前添加它们:
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"POOL_ID", // Identity pool ID
Regions.US_EAST_1 // Region
);
#5
0
System.out.println("Link : " + s3Object.getObjectContent().getHttpRequest().getURI());
with this you can retrieve the link of already uploaded file to S3 bucket.
使用此功能,您可以检索已上载文件到S3存储桶的链接。
#6
0
Similarly if you want link through s3Client you can use below.
同样,如果您想通过s3Client链接,可以在下面使用。
System.out.println("filelink: " + s3Client.getUrl("your_bucket_name", "your_file_key"));