Search in sources :

Example 21 with OSSClient

use of com.aliyun.oss.OSSClient in project DataX by alibaba.

the class OssUtil method initOssClient.

public static OSSClient initOssClient(Configuration conf) {
    String endpoint = conf.getString(Key.ENDPOINT);
    String accessId = conf.getString(Key.ACCESSID);
    String accessKey = conf.getString(Key.ACCESSKEY);
    ClientConfiguration ossConf = new ClientConfiguration();
    ossConf.setSocketTimeout(Constant.SOCKETTIMEOUT);
    // .aliyun.com, if you are .aliyun.ga you need config this
    String cname = conf.getString(Key.CNAME);
    if (StringUtils.isNotBlank(cname)) {
        List<String> cnameExcludeList = new ArrayList<String>();
        cnameExcludeList.add(cname);
        ossConf.setCnameExcludeList(cnameExcludeList);
    }
    OSSClient client = null;
    try {
        client = new OSSClient(endpoint, accessId, accessKey, ossConf);
    } catch (IllegalArgumentException e) {
        throw DataXException.asDataXException(OssReaderErrorCode.ILLEGAL_VALUE, e.getMessage());
    }
    return client;
}
Also used : OSSClient(com.aliyun.oss.OSSClient) ArrayList(java.util.ArrayList) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 22 with OSSClient

use of com.aliyun.oss.OSSClient in project DataX by alibaba.

the class OssUtil method initOssClient.

public static OSSClient initOssClient(Configuration conf) {
    String endpoint = conf.getString(Key.ENDPOINT);
    String accessId = conf.getString(Key.ACCESSID);
    String accessKey = conf.getString(Key.ACCESSKEY);
    ClientConfiguration ossConf = new ClientConfiguration();
    ossConf.setSocketTimeout(Constant.SOCKETTIMEOUT);
    // .aliyun.com, if you are .aliyun.ga you need config this
    String cname = conf.getString(Key.CNAME);
    if (StringUtils.isNotBlank(cname)) {
        List<String> cnameExcludeList = new ArrayList<String>();
        cnameExcludeList.add(cname);
        ossConf.setCnameExcludeList(cnameExcludeList);
    }
    OSSClient client = null;
    try {
        client = new OSSClient(endpoint, accessId, accessKey, ossConf);
    } catch (IllegalArgumentException e) {
        throw DataXException.asDataXException(OssWriterErrorCode.ILLEGAL_VALUE, e.getMessage());
    }
    return client;
}
Also used : OSSClient(com.aliyun.oss.OSSClient) ArrayList(java.util.ArrayList) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 23 with OSSClient

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

the class OssObjectSummaryIteratorTest method makeMockClient.

/**
 * Makes a mock OSS client that handles enough of "listObjects" to test the functionality of the
 * {@link OssObjectSummaryIterator} class.
 */
private static OSS makeMockClient(final List<OSSObjectSummary> objects) {
    return new OSSClient("endpoint", "accessKey", "keySecret") {

        @Override
        public ObjectListing listObjects(final ListObjectsRequest request) {
            // Continuation token is an index in the "objects" list.q
            final String continuationToken = request.getMarker();
            final int startIndex = continuationToken == null ? 0 : Integer.parseInt(continuationToken);
            // Find matching objects.
            final List<OSSObjectSummary> summaries = new ArrayList<>();
            int nextIndex = -1;
            for (int i = startIndex; i < objects.size(); i++) {
                final OSSObjectSummary summary = objects.get(i);
                if (summary.getBucketName().equals(request.getBucketName()) && summary.getKey().startsWith(request.getPrefix())) {
                    if (summaries.size() == request.getMaxKeys()) {
                        // We reached our max key limit; set nextIndex (which will lead to a result with truncated = true).
                        nextIndex = i;
                        break;
                    }
                    // Generate a summary.
                    summaries.add(summary);
                }
            }
            // Generate the result.
            final ObjectListing retVal = new ObjectListing();
            retVal.getObjectSummaries().addAll(summaries);
            if (nextIndex >= 0) {
                retVal.setTruncated(true);
                retVal.setNextMarker(String.valueOf(nextIndex));
            }
            return retVal;
        }
    };
}
Also used : ListObjectsRequest(com.aliyun.oss.model.ListObjectsRequest) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) OSSClient(com.aliyun.oss.OSSClient) ArrayList(java.util.ArrayList) ObjectListing(com.aliyun.oss.model.ObjectListing)

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