Search in sources :

Example 11 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class GetObjectTest method testGetObjectWithSpecialChars.

@Test
public void testGetObjectWithSpecialChars() {
    final String key = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/.zip";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    // TODO: With chinese characters will be failed.
    final String metaKey0 = "tag";
    final String metaValue0 = "元值0";
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
        metadata.addUserMetadata(metaKey0, metaValue0);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, genFixedLengthInputStream(inputStreamLength), metadata);
        ossClient.putObject(putObjectRequest);
        // Override 1
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        OSSObject o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(bucketName, o.getBucketName());
        Assert.assertEquals(key, o.getKey());
        metadata = o.getObjectMetadata();
        Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, metadata.getContentType());
        Assert.assertEquals(metaValue0, metadata.getUserMetadata().get(metaKey0));
        Assert.assertEquals(inputStreamLength, metadata.getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project zeppelin by apache.

the class OSSNotebookRepo method save.

@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
    String content = note.toJson();
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, rootFolder + "/" + buildNoteFileName(note.getId(), note.getPath()), new ByteArrayInputStream(content.getBytes()));
    ossClient.putObject(putObjectRequest);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 13 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project druid by druid-io.

the class OssUtils method uploadFileIfPossible.

/**
 * Uploads a file to aliyun OSS if possible. First trying to set ACL to give the bucket owner full control of the file before uploading.
 *
 * @param client     aliyun OSS client
 * @param key        The key under which to store the new object.
 * @param file       The path of the file to upload to aliyun OSS.
 */
static void uploadFileIfPossible(OSS client, String bucket, String key, File file) {
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, file);
    log.info("Pushing [%s] to bucket[%s] and key[%s].", file, bucket, key);
    client.putObject(putObjectRequest);
}
Also used : PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 14 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project jeesuite-libs by vakinge.

the class AliyunossProvider method upload.

@Override
public String upload(UploadObject object) {
    try {
        PutObjectRequest request = null;
        if (object.getFile() != null) {
            request = new PutObjectRequest(bucketName, object.getFileName(), object.getFile());
        } else if (object.getBytes() != null) {
            request = new PutObjectRequest(bucketName, object.getFileName(), new ByteArrayInputStream(object.getBytes()));
        } else if (object.getInputStream() != null) {
            request = new PutObjectRequest(bucketName, object.getFileName(), object.getInputStream());
        } else {
            throw new IllegalArgumentException("upload object is NULL");
        }
        PutObjectResult result = ossClient.putObject(request);
        if (result.getResponse() == null) {
            return isPrivate ? object.getFileName() : urlprefix + object.getFileName();
        }
        if (result.getResponse().isSuccessful()) {
            return result.getResponse().getUri();
        } else {
            throw new RuntimeException(result.getResponse().getErrorResponseAsString());
        }
    } catch (OSSException e) {
        throw new RuntimeException(e.getErrorMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectResult(com.aliyun.oss.model.PutObjectResult) OSSException(com.aliyun.oss.OSSException) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 15 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest in project aliyun-oss-java-sdk by aliyun.

the class PutObjectTest method testOverridedPutObject.

@Test
public void testOverridedPutObject() throws Exception {
    String key = "overrided-put-object";
    final int instreamLength = 128 * 1024;
    InputStream instream = null;
    try {
        // Override 1
        instream = genFixedLengthInputStream(instreamLength);
        ossClient.putObject(bucketName, key, instream);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        // Override 2
        final String filePath = genFixedLengthFile(instreamLength);
        ossClient.putObject(bucketName, key, new File(filePath));
        Assert.assertEquals(instreamLength, new File(filePath).length());
        // Override 3
        ossClient.putObject(new PutObjectRequest(bucketName, key, new File(filePath)));
        o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) TestUtils.genRandomLengthFile(com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) TestUtils.removeFile(com.aliyun.oss.integrationtests.TestUtils.removeFile) File(java.io.File) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)24 OSSException (com.aliyun.oss.OSSException)18 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectResult (com.aliyun.oss.model.PutObjectResult)9 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)8 Callback (com.aliyun.oss.model.Callback)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)8 InputStream (java.io.InputStream)8 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)7 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)7 IOException (java.io.IOException)6 ClientException (com.aliyun.oss.ClientException)5 OSS (com.aliyun.oss.OSS)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 ParseException (java.text.ParseException)3 TestUtils.genRandomLengthFile (com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile)2 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)2