use of com.aliyun.oss.model.DeleteObjectsRequest in project aliyun-oss-java-sdk by aliyun.
the class ListObjectsSample method main.
public static void main(String[] args) throws IOException {
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
final String content = "Hello OSS";
final String keyPrefix = "MyObjectKey";
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName);
}
// Prepare the environment---inserting 100 test files.
List<String> keys = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
String key = keyPrefix + i;
InputStream instream = new ByteArrayInputStream(content.getBytes());
client.putObject(bucketName, key, instream);
keys.add(key);
}
System.out.println("Put " + keys.size() + " objects completed.");
ObjectListing objectListing = null;
String nextMarker = null;
final int maxKeys = 30;
List<OSSObjectSummary> sums = null;
// use default parameter to list the files. By default up to 100 entries could be listed.
System.out.println("Default paramters:");
objectListing = client.listObjects(bucketName);
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Sets the max keys with 200 (the max value could be 1000).
System.out.println("With max keys:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(200));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified prefix. By default it returns up to 100 entries.
System.out.println("With prefix:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified marker. By default it returns up to 100 entries.
System.out.println("With marker: ");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(keyPrefix + "11"));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets all object by paging. Each page will have up to 100 entries.
System.out.println("List all objects:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified prefix by paging. Each page will have up to 100 entries.
System.out.println("List all objects after marker:");
nextMarker = keyPrefix + "11";
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified marker by paging. Each page will have up to 100 entries.
System.out.println("List all objects with prefix:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix + "1").withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Clean up the environment----deleting the test files.
System.out.println("Deleting all objects:");
DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
for (String object : deletedObjects) {
System.out.println("\t" + object);
}
} 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.
*/
client.shutdown();
}
}
use of com.aliyun.oss.model.DeleteObjectsRequest in project aliyun-oss-java-sdk by aliyun.
the class TestBase method deleteBucketWithObjects.
protected static void deleteBucketWithObjects(OSSClient client, String bucketName) {
if (!client.doesBucketExist(bucketName)) {
return;
}
// delete objects
List<String> allObjects = listAllObjects(client, bucketName);
int total = allObjects.size();
if (total > 0) {
int opLoops = total / DELETE_OBJECTS_ONETIME_LIMIT;
if (total % DELETE_OBJECTS_ONETIME_LIMIT != 0) {
opLoops++;
}
List<String> objectsToDel = null;
for (int i = 0; i < opLoops; i++) {
int fromIndex = i * DELETE_OBJECTS_ONETIME_LIMIT;
int len = 0;
if (total <= DELETE_OBJECTS_ONETIME_LIMIT) {
len = total;
} else {
len = (i + 1 == opLoops) ? (total - fromIndex) : DELETE_OBJECTS_ONETIME_LIMIT;
}
objectsToDel = allObjects.subList(fromIndex, fromIndex + len);
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
deleteObjectsRequest.setEncodingType(DEFAULT_ENCODING_TYPE);
deleteObjectsRequest.setKeys(objectsToDel);
client.deleteObjects(deleteObjectsRequest);
}
}
// delete live channels
List<LiveChannel> channels = ossClient.listLiveChannels(bucketName);
for (LiveChannel channel : channels) {
ossClient.deleteLiveChannel(bucketName, channel.getName());
}
// delete bucket
client.deleteBucket(bucketName);
}
use of com.aliyun.oss.model.DeleteObjectsRequest in project aliyun-oss-java-sdk by aliyun.
the class DeleteObjectsTest method testDeleleObjectsExceedLimit.
@Test
public void testDeleleObjectsExceedLimit() {
List<String> existingKeys = new ArrayList<String>();
final int keyCount = DELETE_OBJECTS_ONETIME_LIMIT + 1;
final String keyPrefix = "delete-objects-exceed-limit";
for (int i = 0; i < keyCount; i++) {
existingKeys.add(keyPrefix + i);
}
DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
try {
request.setKeys(existingKeys);
ossClient.deleteObjects(request);
Assert.fail("Delete objects should not be successfully");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
}
use of com.aliyun.oss.model.DeleteObjectsRequest in project aliyun-oss-java-sdk by aliyun.
the class DeleteObjectsTest method testDeleleObjectsQuietly.
@Test
public void testDeleleObjectsQuietly() {
List<String> existingKeys = new ArrayList<String>();
final int keyCount = 100;
final String keyPrefix = "delete-objects-quietly";
for (int i = 0; i < keyCount; i++) {
existingKeys.add(keyPrefix + i);
}
if (!batchPutObject(ossClient, bucketName, existingKeys)) {
Assert.fail("batch put object failed");
}
DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
request.setQuiet(true);
request.setKeys(existingKeys);
try {
DeleteObjectsResult result = ossClient.deleteObjects(request);
List<String> deletedObjects = result.getDeletedObjects();
Assert.assertEquals(0, deletedObjects.size());
Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.DeleteObjectsRequest in project aliyun-oss-java-sdk by aliyun.
the class GetRequestIdTest method testNormalGetRequestId.
@Test
public void testNormalGetRequestId() {
final String key = "normal-get-request-id";
final long inputStreamLength = 1024;
final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
try {
// put object
PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
// get object
OSSObject ossObject = ossClient.getObject(bucketName, key);
ossObject.getObjectContent().close();
Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
File file = new File("tmp");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
// delete object
ossClient.deleteObject(bucketName, key);
// append object
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
// getSimplifiedObjectMeta
SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
// getObjectMetadata
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
// delete objects
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
ArrayList<String> keys = new ArrayList<String>();
keys.add(key);
deleteObjectsRequest.setKeys(keys);
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
// initiate multipart upload
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
// upload part
UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
// complete multipart upload
List<PartETag> partETags = new ArrayList<PartETag>();
partETags.add(new PartETag(1, uploadPartResult.getETag()));
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
// copy object
CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
// initiate multipart copy
InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
// upload part copy
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
// abort multipart upload
AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
ossClient.deleteObject(bucketName, key);
file.delete();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations