use of com.aliyun.oss.model.ObjectAcl in project aliyun-oss-java-sdk by aliyun.
the class ObjectAclTest method testAppendObjectWithACLHeader.
@Test
public void testAppendObjectWithACLHeader() throws IOException {
final String key = "append-object-with-acl-header";
// 128KB
final long inputStreamLength = 128 * 1024;
try {
// Append at first
InputStream instream = genFixedLengthInputStream(inputStreamLength);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setObjectAcl(CannedAccessControlList.PublicReadWrite);
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, metadata);
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
OSSObject o = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, o.getKey());
Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
if (appendObjectResult.getNextPosition() != null) {
Assert.assertEquals(inputStreamLength, appendObjectResult.getNextPosition().longValue());
}
// Append at twice
final String filePath = genFixedLengthFile(inputStreamLength);
appendObjectRequest = new AppendObjectRequest(bucketName, key, new File(filePath));
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectResult = ossClient.appendObject(appendObjectRequest);
o = ossClient.getObject(bucketName, key);
Assert.assertEquals(inputStreamLength * 2, o.getObjectMetadata().getContentLength());
Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
if (appendObjectResult.getNextPosition() != null) {
Assert.assertEquals(inputStreamLength * 2, appendObjectResult.getNextPosition().longValue());
}
// Verify uploaded objects acl
ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
Assert.assertEquals(ObjectPermission.PublicReadWrite, returnedACL.getPermission());
Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.ObjectAcl in project aliyun-oss-java-sdk by aliyun.
the class ObjectAclTest method testCopyObjectWithACLHeader.
@Test
public void testCopyObjectWithACLHeader() throws IOException {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "copy-existing-object-source-object";
final String targetKey = "copy-existing-object-target-object";
final String userMetaKey0 = "user";
final String userMetaValue0 = "aliy";
final String userMetaKey1 = "tag";
final String userMetaValue1 = "copy-object";
final String contentType = "application/txt";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.length);
metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
metadata.addUserMetadata(userMetaKey0, userMetaValue0);
PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentLength(content.length);
newObjectMetadata.setContentType(contentType);
newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
newObjectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult copyObjectResult = ossClient.copyObject(copyObjectRequest);
String sourceETag = putObjectResult.getETag();
String targetETag = copyObjectResult.getETag();
Assert.assertEquals(sourceETag, targetETag);
Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(contentType, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
// Verify uploaded objects acl
ObjectAcl returnedACL = ossClient.getObjectAcl(targetBucket, targetKey);
Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
waitForCacheExpiration(5);
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.ObjectAcl in project aliyun-oss-java-sdk by aliyun.
the class ResponseParsers method parseGetObjectAcl.
/**
* Unmarshall object acl response body to object ACL.
*/
public static ObjectAcl parseGetObjectAcl(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
ObjectAcl acl = new ObjectAcl();
String id = root.getChild("Owner").getChildText("ID");
String displayName = root.getChild("Owner").getChildText("DisplayName");
Owner owner = new Owner(id, displayName);
acl.setOwner(owner);
String grantString = root.getChild("AccessControlList").getChildText("Grant");
acl.setPermission(ObjectPermission.parsePermission(grantString));
return acl;
} catch (JDOMParseException e) {
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}
use of com.aliyun.oss.model.ObjectAcl in project aliyun-oss-java-sdk by aliyun.
the class GetStartedSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
System.out.println("Getting Started with OSS SDK for Java\n");
try {
/*
* Determine whether the bucket exists
*/
if (!ossClient.doesBucketExist(bucketName)) {
/*
* Create a new OSS bucket
*/
System.out.println("Creating bucket " + bucketName + "\n");
ossClient.createBucket(bucketName);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
}
/*
* List the buckets in your account
*/
System.out.println("Listing buckets");
ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
listBucketsRequest.setMaxKeys(500);
for (Bucket bucket : ossClient.listBuckets()) {
System.out.println(" - " + bucket.getName());
}
System.out.println();
/*
* Upload an object to your bucket
*/
System.out.println("Uploading a new object to OSS from a file\n");
ossClient.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
/*
* Determine whether an object residents in your bucket
*/
boolean exists = ossClient.doesObjectExist(bucketName, key);
System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, key);
System.out.println("ACL:" + objectAcl.getPermission().toString());
/*
* Download an object from your bucket
*/
System.out.println("Downloading an object");
OSSObject object = ossClient.getObject(bucketName, key);
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
displayTextInputStream(object.getObjectContent());
/*
* List objects in your bucket by prefix
*/
System.out.println("Listing objects");
ObjectListing objectListing = ossClient.listObjects(bucketName, "My");
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")");
}
System.out.println();
/*
* Delete an object
*/
System.out.println("Deleting an object\n");
ossClient.deleteObject(bucketName, key);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
ossClient.shutdown();
}
}
Aggregations