我正在创建一个大型 excel 文件,并在使用 IBM Cloud Object Storage 的 Java SDK 上传它时,我得到以下信息
com.ibm.cloud.objectstorage.services.s3.model.AmazonS3Exception: Request Entity Too Large (Service: Amazon S3; Status Code: 413; Error Code: 413 Request Entity Too Large; Request ID: null)
我在做什么?我使用 Apache POI 库创建了一个 excel 文件,现在我想将此对象输出流传输到 IBM COS 存储桶。我怎样才能做到这一点?
使用流的代码
public boolean uploadWorkbookObjectToBucket(Workbook wb, String fileName) {
try {
// converting the workbook to serialized output stream
ByteArrayOutputStream theBytes = new ByteArrayOutputStream(); // create a new output stream to store the object data
ObjectOutputStream serializer = new ObjectOutputStream(theBytes); // set the object data to be serialized
wb.write(serializer); // serialize the object data
serializer.flush(); // flushing
serializer.close(); // closing the serializer
InputStream stream = new ByteArrayInputStream(theBytes.toByteArray()); // convert the serialized data to a new input stream to store
// creating the metadata to upload the data
ObjectMetadata metadata = new ObjectMetadata(); // define the metadata
//metadata.setContentType("application/x-java-serialized-object"); // set the metadata
//metadata.setContentLength(theBytes.size()); // set metadata for the length of the data stream
PutObjectResult result = cos.putObject(bucketName, fileName, stream, metadata);
if(result != null) {
return true;
}
} catch(Exception e) {
logger.error("Exception while uploading file to bucket using the workbook", e);
}
return false;
}
使用传输管理器的代码
private TransferManager getTransferManager() {
return TransferManagerBuilder.standard()
.withS3Client(cos)
.withMinimumUploadPartSize(partSize)
.withMultipartCopyThreshold(thresholdSize)
.build();
}
public boolean uploadFileUsingTransferManager(Workbook wb, String fileName) throws Exception {
// converting the workbook to serialized output stream
ByteArrayOutputStream theBytes = new ByteArrayOutputStream(); // create a new output stream to store the object data
ObjectOutputStream serializer = new ObjectOutputStream(theBytes); // set the object data to be serialized
wb.write(serializer); // serialize the object data
serializer.flush(); // flushing
serializer.close(); // closing the serializer
InputStream stream = new ByteArrayInputStream(theBytes.toByteArray()); // convert the serialized data to a new input stream to store
// creating the metadata to upload the data
ObjectMetadata metadata = new ObjectMetadata(); // define the metadata
// metadata.setContentType("application/x-java-serialized-object"); // set the metadata
// metadata.setContentLength(theBytes.size()); // set metadata for the length of the data stream
TransferManager transferManager = getTransferManager();
try {
Upload transfer = transferManager.upload(bucketName, fileName, stream, metadata);
transfer.waitForCompletion();
// transfer.addProgressListener(new P);
return true;
} catch(Exception e) {
logger.error("Exception while uploading file to bucket using the workbook", e);
} finally {
transferManager.shutdownNow();
wb.close();
}
return false;
}