use of com.aliyun.oss.model.PutObjectResult in project hadoop by apache.
the class AliyunOSSFileSystemStore method uploadObject.
/**
* Upload a file as an OSS object, using single upload.
*
* @param key object key.
* @param file local file to upload.
* @throws IOException if failed to upload object.
*/
public void uploadObject(String key, File file) throws IOException {
File object = file.getAbsoluteFile();
FileInputStream fis = new FileInputStream(object);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(object.length());
if (StringUtils.isNotEmpty(serverSideEncryptionAlgorithm)) {
meta.setServerSideEncryption(serverSideEncryptionAlgorithm);
}
try {
PutObjectResult result = ossClient.putObject(bucketName, key, fis, meta);
LOG.debug(result.getETag());
statistics.incrementWriteOps(1);
} finally {
fis.close();
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperation method putObject.
/**
* Upload input stream or file to oss.
*/
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws OSSException, ClientException {
assertParameterNotNull(putObjectRequest, "putObjectRequest");
PutObjectResult result = null;
if (!isNeedReturnResponse(putObjectRequest)) {
result = writeObjectInternal(WriteMode.OVERWRITE, putObjectRequest, putObjectReponseParser);
} else {
result = writeObjectInternal(WriteMode.OVERWRITE, putObjectRequest, putObjectProcessReponseParser);
}
if (isCrcCheckEnabled()) {
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
}
return result;
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperation method putObject.
/**
* Upload input stream to oss by using url signature.
*/
public PutObjectResult putObject(URL signedUrl, InputStream requestContent, long contentLength, Map<String, String> requestHeaders, boolean useChunkEncoding) throws OSSException, ClientException {
assertParameterNotNull(signedUrl, "signedUrl");
assertParameterNotNull(requestContent, "requestContent");
if (requestHeaders == null) {
requestHeaders = new HashMap<String, String>();
}
RequestMessage request = new RequestMessage(null, null);
request.setMethod(HttpMethod.PUT);
request.setAbsoluteUrl(signedUrl);
request.setUseUrlSignature(true);
request.setContent(requestContent);
request.setContentLength(determineInputStreamLength(requestContent, contentLength, useChunkEncoding));
request.setHeaders(requestHeaders);
request.setUseChunkEncoding(useChunkEncoding);
PutObjectResult result = null;
if (requestHeaders.get(OSSHeaders.OSS_HEADER_CALLBACK) == null) {
result = doOperation(request, putObjectReponseParser, null, null, true);
} else {
result = doOperation(request, putObjectProcessReponseParser, null, null, true);
}
if (isCrcCheckEnabled()) {
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
}
return result;
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testUnormalUploadPartCopyByRange.
@Test
public void testUnormalUploadPartCopyByRange() {
final String sourceBucket = "unormal-upload-part-copy-by-range-bucket-source";
final String targetBucket = "unormal-upload-part-copy-by-range-bucket-target";
final String sourceKey = "unormal-upload-part-copy-by-range-object-source";
final String targetKey = "unormal-upload-part-copy-by-range-object-target";
// 128KB
final long partSize = 128 * 1024;
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
// Put object into source bucket
final long inputStreamLength = partSize * 4;
String eTag = null;
try {
InputStream instream = genFixedLengthInputStream(inputStreamLength);
PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, instream, null);
eTag = result.getETag();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Claim upload id for target bucket
String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
// Upload part copy with invalid copy range
final int partNumber = 1;
final long beginIndex = partSize;
List<PartETag> partETags = new ArrayList<PartETag>();
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyRequest.setBeginIndex(beginIndex);
// Illegal copy range([beginIndex, begin + inputStreamLength]), just copy entire object
uploadPartCopyRequest.setPartSize(inputStreamLength);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
PartListing partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(1, partListing.getParts().size());
Assert.assertEquals(targetBucket, partListing.getBucketName());
Assert.assertEquals(targetKey, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
Assert.assertNotNull(partListing.getNextPartNumberMarker());
Assert.assertFalse(partListing.isTruncated());
Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
// Complete multipart upload
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
// Get uploaded object
OSSObject o = ossClient.getObject(targetBucket, targetKey);
final long objectSize = inputStreamLength;
Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class ChunkedEncodingTest method testPutObjectChunked2.
@Ignore
public void testPutObjectChunked2() {
try {
Date expiration = DateUtil.parseRfc822Date("Wed, 12 Mar 2015 03:15:00 GMT");
HttpMethod method = HttpMethod.PUT;
URL signedUrl = client.generatePresignedUrl(bucketName, key, expiration, method);
File f = new File(filePath);
FileInputStream fin = new FileInputStream(f);
Map<String, String> customHeaders = new HashMap<String, String>();
customHeaders.put("x-oss-meta-author", "aliy");
customHeaders.put("x-oss-tag", "byurl");
// Using url signature & chunked encoding to upload specified inputstream.
PutObjectResult result = client.putObject(signedUrl, fin, f.length(), customHeaders, true);
fin = new FileInputStream(f);
byte[] binaryData = IOUtils.readStreamAsByteArray(fin);
String expectedETag = BinaryUtil.encodeMD5(binaryData);
String actualETag = result.getETag();
Assert.assertEquals(expectedETag, actualETag);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations