Search in sources :

Example 1 with PutObjectResult

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

Example 2 with PutObjectResult

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

Example 3 with PutObjectResult

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;
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 4 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 = "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);
    }
}
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 5 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)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)35 OSSException (com.aliyun.oss.OSSException)19 Test (org.junit.Test)19 OSSObject (com.aliyun.oss.model.OSSObject)17 InputStream (java.io.InputStream)14 File (java.io.File)11 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)10 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 FileInputStream (java.io.FileInputStream)9 Callback (com.aliyun.oss.model.Callback)8 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)8 ArrayList (java.util.ArrayList)8 Date (java.util.Date)6 Ignore (org.junit.Ignore)6 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)5 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)5 InconsistentException (com.aliyun.oss.InconsistentException)4 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)4 CopyObjectRequest (com.aliyun.oss.model.CopyObjectRequest)4