如何在Java Spring Boot中获取上传到Amazon S3存储桶的文件的内容类型或扩展名?

huangapple go评论70阅读模式
英文:

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 = &quot;\n&quot; +
&quot;Usage:\n&quot; +
&quot;    &lt;bucketName&gt; &lt;keyName&gt;&gt;\n\n&quot; +
&quot;Where:\n&quot; +
&quot;    bucketName - The Amazon S3 bucket name. \n\n&quot;+
&quot;    keyName - The key name. \n\n&quot;;
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(&quot;The object content type is &quot;+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

huangapple
  • 本文由 发表于 2023年2月18日 14:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定