Can a client set file name and extension programmatically when he PUTs file content to a presigned S3 URL that the service vends out?

核能气质少年 提交于 2020-06-17 13:27:17

问题


Here is the starter code I'm using from the documentation. I'm trying to create a service that vends out Presigned S3 URLs. I use the default settings of GeneratePresignedUrlRequest as below.

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;

import java.io.IOException;
import java.net.URL;

public class GeneratePresignedURL {

    public static void main(String[] args) throws IOException {
        Regions clientRegion = Regions.DEFAULT_REGION;
        String bucketName = "*** Bucket name ***";
        String objectKey = "*** Object key ***";

        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Set the presigned URL to expire after one hour.
            java.util.Date expiration = new java.util.Date();
            long expTimeMillis = expiration.getTime();
            expTimeMillis += 1000 * 60 * 60;
            expiration.setTime(expTimeMillis);

            // Generate the presigned URL.
            System.out.println("Generating pre-signed URL.");
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(bucketName, objectKey)
                            .withMethod(HttpMethod.PUT)
                            .withExpiration(expiration);
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

            System.out.println("Pre-Signed URL: " + url.toString());
        } catch (AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}

Basically, the code above is taken from the AWS documentation - I've only replaced HttpMethod.GET with HttpMethod.PUT since I want my clients to use the PUT method to send write data to URL connection's OutputStream.

My clients keep complaining that they're setting the Content-Type and the Content-Disposition in their PUT request headers, but I've noticed that in my S3 bucket, it gets stored with the correct Content-Type as per the one set in the header. The .img files despite not being stored in the bucket with a file extension opens up correctly, but the .xlsx files must require the correct application to be chosen by the one who is trying to make the download.

Is there anything the clients are doing wrong from their end when they send their file programmatically? or is it an issue with the Presigned S3 URL that I vend out? Is there something the clients have to do or do I have to change how I create my Presigned S3 URL by using .withContentType and .withResponseHeaders as per the documentation here: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/GeneratePresignedUrlRequest.html?

来源:https://stackoverflow.com/questions/61889316/can-a-client-set-file-name-and-extension-programmatically-when-he-puts-file-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!