Reputation: 41
I want to upload a dynamic Excel file in S3 Bucket without storing it in local system.
I have written a code to do so but it is giving error. Please help
public static void uploadToCloud(HSSFWorkbook excelWorkBook) throws IOException
{
AWSCredentials credentials = new BasicAWSCredentials("a-b-c", "d-e-f");
AmazonS3 s3client = new AmazonS3Client(credentials);
for (Bucket bucket : s3client.listBuckets()) {
System.out.println("Bucket Name - " + bucket.getName());
String bucketnameS3 = bucket.getName();
System.out.println("Bucket Creation date - "+bucket.getCreationDate());
//creating local file just for testing. This part of code is working as expected.
try {
FileOutputStream fileOut = new FileOutputStream(new File("C:\\test.xls"));
excelWorkBook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//upload to amazon s3
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
excelWorkBook.write(byteArrayOutputStream);
byteArrayOutputStream.close();
} catch (IOException e) {
// LOGGER.error(e.getMessage());
}
ByteArrayInputStream bi= new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
bi.read(byteArrayOutputStream.toByteArray());
Long contentLength = Long.valueOf(byteArrayOutputStream.toByteArray().length);
ObjectMetadata objectMetaData =new ObjectMetadata();
objectMetaData.setContentType("application/vnd.ms-excel");
objectMetaData.setContentLength(contentLength);
s3client.putObject(new PutObjectRequest(bucketnameS3,"test.xls",bi,objectMetaData) );
bi.close();
}
}
Error:
[12/14/18 13:08:14:436 IST] 000000ee SystemErr R com.amazonaws.SdkClientException: Data read has a different length than the expected:
dataLength=0;
expectedLength=165376;
includeSkipped=false;
in.getClass()=class com.amazonaws.internal.ReleasableInputStream;
markedSupported=true;
marked=0;
resetSinceLastMarked=false;
markCount=1;
resetCount=0
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.util.LengthCheckInputStream.checkLength(LengthCheckInputStream.java:151)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.util.LengthCheckInputStream.read(LengthCheckInputStream.java:109)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:90)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStream.read(MD5DigestCalculatingInputStream.java:128)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:90)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:180)
[12/14/18 13:08:14:437 IST] 000000ee SystemErr R at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:90)
[12/14/18 13:08:14:438 IST] 000000ee SystemErr R at org.apache.http.entity.InputStreamEntity.writeTo(InputStreamEntity.java:142)
[12/14/18 13:08:14:438 IST] 000000ee SystemErr R at com.amazonaws.http.RepeatableInputStreamRequestEntity.writeTo(RepeatableInputStreamRequestEntity.java:160)
[12/14/18 13:08:14:438 IST] 000000ee SystemErr R at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:158)
[12/14/18 13:08:14:438 IST] 000000ee SystemErr R at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:162)
[12/14/18 13:08:14:438 IST] 000000ee SystemErr R at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at com.amazonaws.http.protocol.SdkHttpRequestExecutor.doSendRequest(SdkHttpRequestExecutor.java:63)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
[12/14/18 13:08:14:439 IST] 000000ee SystemErr R at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
[12/14/18 13:08:14:440 IST] 000000ee SystemErr R at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1285)
[12/14/18 13:08:14:440 IST] 000000ee SystemErr R at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1101)
I am using aws-java-sdk-1.11.464.jar
.
Upvotes: 1
Views: 4087
Reputation: 41
The issue has been resolved and my code is working fine now.
bi.read(byteArrayOutputStream.toByteArray());---- This line needs to be removed from the code.
Upvotes: 3