Why does file uploaded to S3 have content type application/octet-stream unless i name the file .html

前端 未结 5 564
悲哀的现实
悲哀的现实 2021-01-17 09:04

Even though I set content type to text/html it ends up as application/octet-stream on S3.

ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(c         


        
5条回答
  •  一整个雨季
    2021-01-17 09:29

    Because you have to set content type at the end just before sending, using the putObject method;

            ObjectMetadata md = new ObjectMetadata();
    
            InputStream myInputStream = new ByteArrayInputStream(bFile); 
            md.setContentLength(bFile.length);
            md.setContentType("text/html");
            md.setContentEncoding("UTF-8");
    
            s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));
    

    And after upload, content type is set as "text/html"

    enter image description here

    Here is a working dummy code, check that out, I've just tried and it's working;

    public class TestAWS {
    
        //TEST
        private static String bucketName = "whateverBucket";
    
        public static void main(String[] args) throws Exception {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials("whatever", "whatever");
    
            AmazonS3 s3client = new AmazonS3Client(awsCreds);
            try
            {
                String uploadFileName = "D:\\try.txt";
                String keyName = "newFile.txt";
    
                System.out.println("Uploading a new object to S3 from a file\n");
                File file = new File(uploadFileName);
    
                //bFile will be the placeholder of file bytes
                byte[] bFile = new byte[(int) file.length()];
                FileInputStream fileInputStream=null;
    
                //convert file into array of bytes  
                fileInputStream = new FileInputStream(file);
                fileInputStream.read(bFile);
                fileInputStream.close();
    
                ObjectMetadata md = new ObjectMetadata();
    
                InputStream myInputStream = new ByteArrayInputStream(bFile); 
                md.setContentLength(bFile.length);
                md.setContentType("text/html");
                md.setContentEncoding("UTF-8");
    
                s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));
            } catch (AmazonServiceException ase)
            {
                System.out.println("Caught an AmazonServiceException, which "
                        + "means your request made it "
                        + "to Amazon S3, but was rejected with an error response"
                        + " for some reason.");
                System.out.println("Error Message:    " + ase.getMessage());
                System.out.println("HTTP Status Code: " + ase.getStatusCode());
                System.out.println("AWS Error Code:   " + ase.getErrorCode());
                System.out.println("Error Type:       " + ase.getErrorType());
                System.out.println("Request ID:       " + ase.getRequestId());
            } catch (AmazonClientException ace)
            {
                System.out.println("Caught an AmazonClientException, which "
                        + "means the client encountered "
                        + "an internal error while trying to "
                        + "communicate with S3, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message: " + ace.getMessage());
            }
    
        }
    
    }
    

    Hope that it helps.

提交回复
热议问题