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();
}
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);
}
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");
}
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;
}
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();
}
}
Aggregations