Search in sources :

Example 1 with OSSClient

use of com.aliyun.oss.OSSClient in project hadoop by apache.

the class AliyunOSSFileSystemStore method initialize.

public void initialize(URI uri, Configuration conf, FileSystem.Statistics stat) throws IOException {
    statistics = stat;
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS_KEY, MAXIMUM_CONNECTIONS_DEFAULT));
    boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS_KEY, SECURE_CONNECTIONS_DEFAULT);
    clientConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP);
    clientConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES_KEY, MAX_ERROR_RETRIES_DEFAULT));
    clientConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT_KEY, ESTABLISH_TIMEOUT_DEFAULT));
    clientConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT_KEY, SOCKET_TIMEOUT_DEFAULT));
    String proxyHost = conf.getTrimmed(PROXY_HOST_KEY, "");
    int proxyPort = conf.getInt(PROXY_PORT_KEY, -1);
    if (StringUtils.isNotEmpty(proxyHost)) {
        clientConf.setProxyHost(proxyHost);
        if (proxyPort >= 0) {
            clientConf.setProxyPort(proxyPort);
        } else {
            if (secureConnections) {
                LOG.warn("Proxy host set without port. Using HTTPS default 443");
                clientConf.setProxyPort(443);
            } else {
                LOG.warn("Proxy host set without port. Using HTTP default 80");
                clientConf.setProxyPort(80);
            }
        }
        String proxyUsername = conf.getTrimmed(PROXY_USERNAME_KEY);
        String proxyPassword = conf.getTrimmed(PROXY_PASSWORD_KEY);
        if ((proxyUsername == null) != (proxyPassword == null)) {
            String msg = "Proxy error: " + PROXY_USERNAME_KEY + " or " + PROXY_PASSWORD_KEY + " set without the other.";
            LOG.error(msg);
            throw new IllegalArgumentException(msg);
        }
        clientConf.setProxyUsername(proxyUsername);
        clientConf.setProxyPassword(proxyPassword);
        clientConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN_KEY));
        clientConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION_KEY));
    } else if (proxyPort >= 0) {
        String msg = "Proxy error: " + PROXY_PORT_KEY + " set without " + PROXY_HOST_KEY;
        LOG.error(msg);
        throw new IllegalArgumentException(msg);
    }
    String endPoint = conf.getTrimmed(ENDPOINT_KEY, "");
    CredentialsProvider provider = AliyunOSSUtils.getCredentialsProvider(conf);
    ossClient = new OSSClient(endPoint, provider, clientConf);
    uploadPartSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    multipartThreshold = conf.getLong(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY, MIN_MULTIPART_UPLOAD_THRESHOLD_DEFAULT);
    partSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    if (partSize < MIN_MULTIPART_UPLOAD_PART_SIZE) {
        partSize = MIN_MULTIPART_UPLOAD_PART_SIZE;
    }
    serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM_KEY, "");
    if (uploadPartSize < 5 * 1024 * 1024) {
        LOG.warn(MULTIPART_UPLOAD_SIZE_KEY + " must be at least 5 MB");
        uploadPartSize = 5 * 1024 * 1024;
    }
    if (multipartThreshold < 5 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be at least 5 MB");
        multipartThreshold = 5 * 1024 * 1024;
    }
    if (multipartThreshold > 1024 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be less than 1 GB");
        multipartThreshold = 1024 * 1024 * 1024;
    }
    String cannedACLName = conf.get(CANNED_ACL_KEY, CANNED_ACL_DEFAULT);
    if (StringUtils.isNotEmpty(cannedACLName)) {
        CannedAccessControlList cannedACL = CannedAccessControlList.valueOf(cannedACLName);
        ossClient.setBucketAcl(bucketName, cannedACL);
    }
    maxKeys = conf.getInt(MAX_PAGING_KEYS_KEY, MAX_PAGING_KEYS_DEFAULT);
    bucketName = uri.getHost();
}
Also used : OSSClient(com.aliyun.oss.OSSClient) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 2 with OSSClient

use of com.aliyun.oss.OSSClient in project FredaBlog by yangjinlong86.

the class OssUtils method getObject.

public static void getObject(String fileName) {
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    OSSObject ossObject = ossClient.getObject(bucketName, fileName);
    InputStream inputStream = ossObject.getObjectContent();
    StringBuilder objectContent = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    while (true) {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (line == null) {
            break;
        }
        objectContent.append(line);
    }
    try {
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
    System.out.println("Object:" + fileName + "的内容是:" + objectContent);
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient)

Example 3 with OSSClient

use of com.aliyun.oss.OSSClient in project FredaBlog by yangjinlong86.

the class OssUtils method test.

public static void test() {
    String firstKey = "jason_test_object";
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    try {
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("您已经创建Bucket:" + bucketName + "。");
        } else {
            System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
            // 创建Bucket。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            ossClient.createBucket(bucketName);
        }
        // 查看Bucket信息。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        System.out.println("Bucket " + bucketName + "的信息如下:");
        System.out.println("\t数据中心:" + info.getBucket().getLocation());
        System.out.println("\t创建时间:" + info.getBucket().getCreationDate());
        System.out.println("\t用户标志:" + info.getBucket().getOwner());
        // 把字符串存入OSS,Object的名称为firstKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        InputStream is = new ByteArrayInputStream("Hello OSS".getBytes());
        ossClient.putObject(bucketName, firstKey, is);
        System.out.println("Object:" + firstKey + "存入OSS成功。");
        // 下载文件。详细请参看“SDK手册 > Java-SDK > 下载文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/download_object.html?spm=5176.docoss/sdk/java-sdk/manage_object
        OSSObject ossObject = ossClient.getObject(bucketName, firstKey);
        InputStream inputStream = ossObject.getObjectContent();
        StringBuilder objectContent = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            objectContent.append(line);
        }
        inputStream.close();
        System.out.println("Object:" + firstKey + "的内容是:" + objectContent);
        // 文件存储入OSS,Object的名称为fileKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        String fileKey = "README.md";
        ossClient.putObject(bucketName, fileKey, new File("README.md"));
        System.out.println("Object:" + fileKey + "存入OSS成功。");
        // 查看Bucket中的Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries();
        System.out.println("您有以下Object:");
        for (OSSObjectSummary object : objectSummary) {
            System.out.println("\t" + object.getKey());
        }
    // 删除Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
    // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
    } catch (OSSException oe) {
        oe.printStackTrace();
    } catch (ClientException ce) {
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
    logger.info("Completed");
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) BucketInfo(com.aliyun.oss.model.BucketInfo) ClientException(com.aliyun.oss.ClientException)

Example 4 with OSSClient

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

the class TestBase method getOSSClient.

public static OSSClient getOSSClient() {
    if (ossClient == null) {
        resetTestConfig();
        ClientConfiguration conf = new ClientConfiguration().setSupportCname(false);
        Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        ossClient = new OSSClient(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(credentials), conf);
    }
    return ossClient;
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials)

Example 5 with OSSClient

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

the class SecurityTokenTest method testObjectOperationsWithToken.

@Test
public void testObjectOperationsWithToken() throws JSONException {
    List<String> actions = new ArrayList<String>();
    List<String> resources = new ArrayList<String>();
    // Put bucket with valid security token
    final String bucketName = "test-object-operations-with-token-bucket0";
    actions.add("oss:PutBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    OSSClient sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put object
    final String key = "test-object-operations-with-token-key0";
    final long instreamLength = 1024;
    InputStream instream = null;
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        instream = genFixedLengthInputStream(instreamLength);
        sessionClient.putObject(bucketName, key, instream, null);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException e) {
            }
        }
        sessionClient.shutdown();
    }
    // Get object
    actions.add("oss:GetObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        OSSObject o = sessionClient.getObject(bucketName, key);
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Copy object
    actions.add("oss:GetObject");
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.copyObject(bucketName, key, bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Initiate multipart upload and upload single part
    // 128KB
    final int partSize = 128 * 1024;
    String uploadId = null;
    List<PartETag> partETags = new ArrayList<PartETag>();
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult result = sessionClient.initiateMultipartUpload(request);
        instream = genFixedLengthInputStream(partSize);
        uploadId = result.getUploadId();
        // Upload single part
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(partSize);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = sessionClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // List parts
    actions.add("oss:ListParts");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        PartListing partListing = sessionClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(1000, partListing.getMaxParts().intValue());
        Assert.assertEquals(1, partListing.getNextPartNumberMarker().intValue());
        Assert.assertFalse(partListing.isTruncated());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Complete multipart
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        sessionClient.completeMultipartUpload(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Cleanup objects and bucket
    actions.add("oss:DeleteObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteObject(bucketName, key);
        sessionClient.deleteObject(bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    actions.add("oss:DeleteBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) IOException(java.io.IOException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Aggregations

OSSClient (com.aliyun.oss.OSSClient)23 Test (org.junit.Test)8 OSSObject (com.aliyun.oss.model.OSSObject)7 ClientConfiguration (com.aliyun.oss.ClientConfiguration)6 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 BucketInfo (com.aliyun.oss.model.BucketInfo)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 OSSException (com.aliyun.oss.OSSException)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)3 Credentials (com.aliyun.oss.common.auth.Credentials)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)2 Bucket (com.aliyun.oss.model.Bucket)2 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)2 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)2 IOException (java.io.IOException)2