publicvoidcreateBucket(String bucketName){ if (!s3Client.doesBucketExistV2(bucketName)) { s3Client.createBucket(new CreateBucketRequest(bucketName)); } // Verify that the bucket was created by retrieving it and checking its location. String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName)); log.info("Bucket location: " + bucketLocation); }
publicvoiddeleteBucket(String bucketName){ if (!s3Client.doesBucketExistV2(bucketName)) { log.info("Bucket " + bucketName + " does not exist."); return; } ObjectListing objectListing = s3Client.listObjects(bucketName); while (true) { Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator(); while (objIter.hasNext()) { s3Client.deleteObject(bucketName, objIter.next().getKey()); } if (objectListing.isTruncated()) { objectListing = s3Client.listNextBatchOfObjects(objectListing); } else { break; } }
VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName)); while (true) { Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator(); while (versionIter.hasNext()) { S3VersionSummary vs = versionIter.next(); s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId()); }
// Upload a file as a new object with ContentType and title specified. log.info("Upload object " + fileName); PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(ORIGINAL_FOLDER + "/" + fileName)); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("plain/video"); metadata.addUserMetadata("x-amz-meta-title", "someTitle"); request.setMetadata(metadata); s3Client.putObject(request); }
publicvoidstoreFile(InputStream input, String fileName)throws IOException { log.info("Store File " + fileName); // Read the text input stream one line at a time and display each line. BufferedInputStream reader = new BufferedInputStream(input); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(DOWNLOAD_FOLDER + "/" + fileName)); byte[] buff = newbyte[1024]; int len = 0; while ((len = reader.read(buff)) != -1) { writer.write(buff, 0, len); } reader.close(); writer.close(); } }