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());
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class ConcurrencyTest method testPutObjectConcurrently.
@Ignore
public void testPutObjectConcurrently() {
final int threadCount = 100;
final String filePath = "D:\\software\\aliw.exe";
try {
Thread[] ts = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
final int seqNum = i;
Runnable r = new Runnable() {
@Override
public void run() {
try {
PutObjectResult result = client.putObject(bucketName, buildObjectKey(key, seqNum), new File(filePath));
// Marks this thread has done.
completedCount.incrementAndGet();
System.out.println(result.getETag());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
};
ts[i] = new Thread(r);
}
for (int i = 0; i < threadCount; i++) {
ts[i].start();
}
for (int i = 0; i < threadCount; i++) {
ts[i].join();
}
} catch (Exception ex) {
Assert.fail(ex.getMessage());
} finally {
int totalCompleted = completedCount.get();
Assert.assertEquals(threadCount, totalCompleted);
}
}
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 = super.bucketName + "-" + "unormal-upload-part-copy-range-source";
final String targetBucket = super.bucketName + "-" + "unormal-upload-part-copy-range-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 testPutObjectChunked.
@Ignore
public void testPutObjectChunked() {
try {
File f = new File(filePath);
FileInputStream fin = new FileInputStream(new File(filePath));
// Without setting Content-Length of inputstream explicitly, using chunked encoding by default.
PutObjectResult result = client.putObject(bucketName, key, fin, new ObjectMetadata());
fin = new FileInputStream(f);
byte[] binaryData = IOUtils.readStreamAsByteArray(fin);
String actualETag = BinaryUtil.encodeMD5(binaryData);
String expectedETag = result.getETag();
Assert.assertEquals(expectedETag, actualETag);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.PutObjectResult in project litemall by linlinjava.
the class AliyunStorage method store.
/**
* 阿里云OSS对象存储简单上传实现
*/
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
try {
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
objectMetadata.setContentType(contentType);
// 对象键(Key)是对象在存储桶中的唯一标识。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
Aggregations