英文:
How to get the content type or extension of file uploaded into an Amazon S3 Bucket in Java Spring boot?
问题
嘿,有人曾经在Amazon S3存储桶上工作过吗?有人可以告诉我如何获取上传到存储桶中特定对象的内容类型吗?
我尝试将对象再次转换为文件并获取扩展名,但没有成功。
英文:
hey anyone worked on Amazon S3 bucket...can anyone tell me how to get the content type of a particular object uploaded into the bucket?
I tried converting the object again into an file and getting the extension but it did not work.
答案1
得分: 1
要获取上传到Amazon S3存储桶的特定对象的内容类型,您可以使用AWS SDK for Java v2。您可以在Spring Boot项目中使用AWS SDK for Java v2。
执行此用例时,您可以使用以下代码示例。当您运行此代码并且对象是一个.jpeg文件时,输出结果为:
对象的内容类型是image/jpeg
Java代码:
public class GetObjectContentType {
public static void main(String[] args) {
final String usage = "\n" +
"Usage:\n" +
" <bucketName> <keyName>\n\n" +
"Where:\n" +
" bucketName - Amazon S3存储桶名称。\n\n" +
" keyName - 键名称。\n\n";
if (args.length != 2) {
System.out.println(usage);
System.exit(1);
}
String bucketName = args[0];
String keyName = args[1];
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.credentialsProvider(credentialsProvider)
.build();
getContentType(s3, bucketName, keyName);
s3.close();
}
// snippet-start:[s3.java2.getobjectcontenttype.main]
public static void getContentType(S3Client s3, String bucketName, String keyName) {
try {
HeadObjectRequest objectRequest = HeadObjectRequest.builder()
.key(keyName)
.bucket(bucketName)
.build();
HeadObjectResponse objectHead = s3.headObject(objectRequest);
String type = objectHead.contentType();
System.out.println("对象的内容类型是" + type);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
// snippet-end:[s3.java2.getobjectcontenttype.main]
}
此代码示例可在AWS代码库中找到:
使用AWS SDK确定Amazon S3存储桶中对象的存在性和内容类型
英文:
To get the content type of a particular object uploaded into an Amazon S3 bucket, you can use the AWS SDK for Java v2. You can use the AWS SDK for Java v2 in a Spring BOOT project.
To perform this use case, you can use the following code example. When you run this and the object is a .jpeg, the output is:
The object content type is image/jpeg
Java code:
public class GetObjectContentType {
public static void main(String[] args) {
final String usage = "\n" +
"Usage:\n" +
" <bucketName> <keyName>>\n\n" +
"Where:\n" +
" bucketName - The Amazon S3 bucket name. \n\n"+
" keyName - The key name. \n\n";
if (args.length != 2) {
System.out.println(usage);
System.exit(1);
}
String bucketName = args[0];
String keyName = args[1];
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.credentialsProvider(credentialsProvider)
.build();
getContentType(s3,bucketName,keyName);
s3.close();
}
// snippet-start:[s3.java2.getobjectcontenttype.main]
public static void getContentType (S3Client s3, String bucketName, String keyName) {
try {
HeadObjectRequest objectRequest = HeadObjectRequest.builder()
.key(keyName)
.bucket(bucketName)
.build();
HeadObjectResponse objectHead = s3.headObject(objectRequest);
String type = objectHead.contentType();
System.out.println("The object content type is "+type);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
// snippet-end:[s3.java2.getobjectcontenttype.main]
}
This code example can be found in the AWS Code Library here:
Determine the existence and content type of an object in an Amazon S3 bucket using an AWS SDK
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论