AmazonClientException: Data read has a different length than the expected

前端 未结 4 1751
礼貌的吻别
礼貌的吻别 2020-12-19 09:56

1.When I am using AmazonS3Client to upload file on amazon s3 file store. 2.when I am trying to upload multiple files at a time it gives exceptions: but same file multiple th

相关标签:
4条回答
  • 2020-12-19 10:09

    This answer was wrote from the guy of AWS Hanson:

    Is it possible that the input stream that is specified in the request has already been fully read?

    If the input stream is a file stream, have you tried specifying the original file in the request instead of the input stream of the file?

    0 讨论(0)
  • 2020-12-19 10:17

    I saw that error message when I was trying to do a S3.putObject(MyObject);

    I had to update objectMetadata.setContentLength( [length of your content] );

    For example:

    String dataset= "Some value you want to add to S3 Bucket"; 
    ObjectMetadata objectMetadata= new ObjectMetadata(); 
    InputStream content= new ByteArrayInputStream(dataset.getBytes("UTF-8"));
    objectMetadata.setContentLength(content.available()); 
    objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYTION); 
    
    0 讨论(0)
  • 2020-12-19 10:18
    ...
    byte[] f = IOUtils.toByteArray(inputStream); // This reads all bytes of the input stream
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(f.length);
    metadata.setContentType(contentType); //Content type of the uploaded file
    metadata.setHeader("filename", fileName);
    s3.putObject(bucketName, key, new ByteArrayInputStream(f), metadata); // Here we create a new ByteArrayInputStream so S3 client get happy.
    
    0 讨论(0)
  • 2020-12-19 10:24

    Improving @iucasddaniel answer with sample code.

    AmazonS3Client putObject: No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors.

    Solution « Specify Object Metadata content Length

    File tempFile = "D://Test.mp4";
    String bucketName = "YashFiles", filePath = "local/mp4/";
    
    FileInputStream sampleStream = new FileInputStream( tempFile );
    byte[] byteArray = IOUtils.toByteArray( sampleStream );
    Long contentLength = Long.valueOf(byteArray.length);
    sampleStream.close();
    
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(contentLength);
    
    TransferManager tm = new TransferManager(credentials);
    
    FileInputStream stream = new FileInputStream( tempFile );
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filePath, stream,objectMetadata);
    Upload myUpload = tm.upload(putObjectRequest);
        if (myUpload.isDone() == false) {
            System.out.println("Transfer: "+ myUpload.getDescription());
            System.out.println("  - State: "+ myUpload.getState());
            System.out.println("  - Progress: "+ myUpload.getProgress().getBytesTransferred());
        }
    myUpload.waitForCompletion();
    
    tm.shutdownNow();
    stream.close();
    
    org.apache.commons.io.FileUtils.forceDelete( tempFile );
    

    Amazon S3: Checking Key Exists and generating PresignedUrl

    0 讨论(0)
提交回复
热议问题