Search in sources :

Example 1 with CreateBucketRequest

use of com.aliyun.oss.model.CreateBucketRequest in project CBEC-B2B by A-Cubic.

the class OSSUtils method uploadOSSToInputStream.

public static void uploadOSSToInputStream(InputStream in, String filename) {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        if (!ossClient.doesBucketExist(bucketName)) {
            System.out.println("Creating bucket " + bucketName + "\n");
            ossClient.createBucket(bucketName);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }
        ossClient.putObject(new PutObjectRequest(bucketName, key + "/" + filename, in));
    } catch (Exception e) {
        logger.error("上传oss失败,原因:" + e.getMessage());
    } finally {
        ossClient.shutdown();
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 2 with CreateBucketRequest

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

the class OSSClientRequestTest method testCreateBucketRequest.

@Test
public void testCreateBucketRequest() {
    TestAction test = new TestAction() {

        @Override
        public void run() throws Exception {
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            createBucketRequest.setLocationConstraint(LocationConstraint.OSS_CN_QINGDAO);
            bucketOp.createBucket(createBucketRequest);
        }
    };
    String requestXml = "<CreateBucketConfiguration><LocationConstraint>oss-cn-qingdao</LocationConstraint></CreateBucketConfiguration>";
    executeTest(test, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), "", null, requestXml, requestXml.length());
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) Test(org.junit.Test)

Example 3 with CreateBucketRequest

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

the class CreateBucketTest method testPutWithStorageTypeAndLocation.

@Test
public void testPutWithStorageTypeAndLocation() {
    final String bucketName = "bucket-with-storage-and-location";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    createBucketRequest.setStorageClass(StorageClass.Standard);
    createBucketRequest.setLocationConstraint(OSS_TEST_REGION);
    try {
        ossClient.createBucket(createBucketRequest);
        AccessControlList returnedAcl = ossClient.getBucketAcl(bucketName);
        Set<Grant> grants = returnedAcl.getGrants();
        Assert.assertEquals(0, grants.size());
        System.out.println(returnedAcl.toString());
        BucketList buckets = ossClient.listBuckets(bucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
        Assert.assertEquals(StorageClass.Standard, buckets.getBucketList().get(0).getStorageClass());
        Assert.assertEquals(OSS_TEST_REGION, buckets.getBucketList().get(0).getLocation());
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 4 with CreateBucketRequest

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

the class CreateBucketTest method testPutWithStorageTypeCompatibility.

@Test
public void testPutWithStorageTypeCompatibility() {
    final String bucketName = "bucket-with-storage-type-compatibility";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    try {
        ossClient.createBucket(createBucketRequest);
        BucketList buckets = ossClient.listBuckets(bucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
        Assert.assertEquals(StorageClass.Standard, buckets.getBucketList().get(0).getStorageClass());
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 5 with CreateBucketRequest

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

the class CreateBucketTest method testPutWithDefaultStorageType.

@Test
public void testPutWithDefaultStorageType() {
    final String bucketName = "bucket-with-default-storage-type";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    try {
        ossClient.createBucket(createBucketRequest);
        BucketList buckets = ossClient.listBuckets(bucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
        Assert.assertEquals(StorageClass.Standard, buckets.getBucketList().get(0).getStorageClass());
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)17 OSSException (com.aliyun.oss.OSSException)15 Test (org.junit.Test)11 BucketList (com.aliyun.oss.model.BucketList)6 AccessControlList (com.aliyun.oss.model.AccessControlList)4 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)4 Grant (com.aliyun.oss.model.Grant)4 Ignore (org.junit.Ignore)4 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)3 File (java.io.File)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSObject (com.aliyun.oss.model.OSSObject)2 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)2 ClientException (com.aliyun.oss.ClientException)1 Bucket (com.aliyun.oss.model.Bucket)1 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1