Search in sources :

Example 21 with PutObjectResult

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());
    }
}
Also used : HashMap(java.util.HashMap) PutObjectResult(com.aliyun.oss.model.PutObjectResult) File(java.io.File) Date(java.util.Date) HttpMethod(com.aliyun.oss.HttpMethod) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore)

Example 22 with PutObjectResult

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);
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) File(java.io.File) IOException(java.io.IOException) Ignore(org.junit.Ignore)

Example 23 with PutObjectResult

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);
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 24 with PutObjectResult

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());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore)

Example 25 with PutObjectResult

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);
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) MalformedURLException(java.net.MalformedURLException)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)65 Test (org.junit.Test)31 OSSException (com.aliyun.oss.OSSException)30 InputStream (java.io.InputStream)24 OSSObject (com.aliyun.oss.model.OSSObject)20 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)19 File (java.io.File)15 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)13 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 FileInputStream (java.io.FileInputStream)9 Callback (com.aliyun.oss.model.Callback)8 IOException (java.io.IOException)8 OSS (com.aliyun.oss.OSS)7 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)7 Date (java.util.Date)7 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)6 Ignore (org.junit.Ignore)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5