/** * Returns the names of buckets belonging to the service user * * @return the list of buckets names owned by the service user. * @throws SdkClientException */ public String[] getBucketsNames() throws SdkClientException { return getBuckets().stream().map( b -> b.getName() ).toArray( String[]::new ); }
/** * Returns an object representing the details of an item in S3. The object is returned without the object's data. * * @param bucket * the bucket containing the object. * @param objectKey * the key identifying the object. * @return the object with the given key in S3, including only general details and metadata (not the data input * stream) * @throws SdkClientException */ private ObjectMetadata getS3ObjectDetails( Bucket bucket, String objectKey ) throws SdkClientException { return s3Client.getObjectMetadata( bucket.getName(), objectKey ); }
private static void ListMyBuckets() { List<Bucket> buckets = s3.listBuckets(); System.out.println("My buckets now are:"); for (Bucket b : buckets) { System.out.println(b.getName()); } }
/** * Returns the objects in a bucket. The objects returned by this method contain only minimal information. * * @param bucket * the bucket whose contents will be listed. * @return the set of objects contained in a bucket. * @throws SdkClientException */ private ObjectListing getS3Objects( Bucket bucket ) throws SdkClientException { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); return s3Client.listObjects( bucket.getName() ); } finally { Thread.currentThread().setContextClassLoader( currentClassLoader ); } }
public static void main(String[] args) { final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); List<Bucket> buckets = s3.listBuckets(); System.out.println("Your Amazon S3 buckets are:"); for (Bucket b : buckets) { System.out.println("* " + b.getName()); } } }
/** * @see java.lang.Object#toString() */ public String toString() { return "S3Bucket [name=" + getName() + ", creationDate=" + getCreationDate() + ", owner=" + getOwner() + "]"; }
public static Bucket getBucket(String bucket_name) { final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); Bucket named_bucket = null; List<Bucket> buckets = s3.listBuckets(); for (Bucket b : buckets) { if (b.getName().equals(bucket_name)) { named_bucket = b; } } return named_bucket; }
protected JsonArray serveS3(String filter, int limit){ JsonArray array = new JsonArray(); try { AmazonS3 s3 = PersistS3.getClient(); filter = Strings.nullToEmpty(filter); for( Bucket b : s3.listBuckets() ) { if( b.getName().startsWith(filter) ) array.add(new JsonPrimitive(b.getName())); if( array.size() == limit) break; } } catch( IllegalArgumentException xe ) { } return array; }
@Override protected JsonArray serve(String filter, int limit) { JsonArray array = new JsonArray(); try { AmazonS3 s3 = PersistS3.getClient(); filter = Strings.nullToEmpty(filter); for( Bucket b : s3.listBuckets() ) { if( b.getName().startsWith(filter) ) array.add(new JsonPrimitive(b.getName())); if( array.size() == limit) break; } } catch( IllegalArgumentException xe ) { } return array; } }
private Bucket getBucket(String bucket_name) { final AmazonS3 s3 = s3Client(); Bucket named_bucket = null; List<Bucket> buckets = s3.listBuckets(); for (Bucket b : buckets) { if (b.getName().equals(bucket_name)) { named_bucket = b; } } if (named_bucket == null) { named_bucket = createBucket(bucket_name); } return named_bucket; }
private Bucket getBucket(String bucket_name) { final AmazonS3 s3 = s3Client(); Bucket named_bucket = null; List<Bucket> buckets = s3.listBuckets(); for (Bucket b : buckets) { if (b.getName().equals(bucket_name)) { named_bucket = b; } } if (named_bucket == null) { named_bucket = createBucket(bucket_name); } return named_bucket; }
/** * Returns an object representing the details and data of an item in S3. * * @param bucket * the bucket containing the object. * @param objectKey * the key identifying the object. * @param byteRangeStart * include only a portion of the object's data - starting at this point * @param byteRangeEnd * include only a portion of the object's data - ending at this point * @return the object with the given key in S3, including details and data * @throws SdkClientException */ public S3Object getS3Object( Bucket bucket, String objectKey, Long byteRangeStart, Long byteRangeEnd ) throws SdkClientException { if ( byteRangeStart != null && byteRangeEnd != null ) { GetObjectRequest rangeObjectRequest = new GetObjectRequest( bucket.getName(), objectKey ).withRange( byteRangeStart, byteRangeEnd ); return s3Client.getObject( rangeObjectRequest ); } else { return s3Client.getObject( bucket.getName(), objectKey ); } }
@Test public void s3Test() throws IOException { AmazonS3 s3 = AmazonS3ClientBuilder .standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(S3)) .withCredentials(localstack.getDefaultCredentialsProvider()) .build(); s3.createBucket("foo"); s3.putObject("foo", "bar", "baz"); final List<Bucket> buckets = s3.listBuckets(); assertEquals("The created bucket is present", 1, buckets.size()); final Bucket bucket = buckets.get(0); assertEquals("The created bucket has the right name", "foo", bucket.getName()); assertEquals("The created bucket has the right name", "foo", bucket.getName()); final ObjectListing objectListing = s3.listObjects("foo"); assertEquals("The created bucket has 1 item in it", 1, objectListing.getObjectSummaries().size()); final S3Object object = s3.getObject("foo", "bar"); final String content = IOUtils.toString(object.getObjectContent(), Charset.forName("UTF-8")); assertEquals("The object can be retrieved", "baz", content); } }
/** * @see java.lang.Object#toString() */ @Override public String toString() { return "S3Bucket [name=" + getName() + ", creationDate=" + getCreationDate() + ", owner=" + getOwner() + "]"; }
private List<String> findMatchingBuckets(String bucketPattern) { List<Bucket> buckets = this.amazonS3.listBuckets(); List<String> matchingBuckets = new ArrayList<>(); for (Bucket bucket : buckets) { this.amazonS3.getBucketLocation(bucket.getName()); if (this.pathMatcher.match(bucketPattern, bucket.getName())) { matchingBuckets.add(bucket.getName()); } } return matchingBuckets; }
private List<String> findMatchingBuckets(String bucketPattern) { List<Bucket> buckets = this.amazonS3.listBuckets(); List<String> matchingBuckets = new ArrayList<>(); for (Bucket bucket : buckets) { this.amazonS3.getBucketLocation(bucket.getName()); if (this.pathMatcher.match(bucketPattern, bucket.getName())) { matchingBuckets.add(bucket.getName()); } } return matchingBuckets; }
/** * @see java.lang.Object#toString() */ @Override public String toString() { return "S3Bucket [name=" + getName() + ", creationDate=" + getCreationDate() + ", owner=" + getOwner() + "]"; }
@SuppressWarnings("unchecked") @Override public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) { if (type != S3FileStoreAttributeView.class) throw new IllegalArgumentException("FileStoreAttributeView of type '" + type.getName() + "' is not supported."); Bucket buck = getBucket(); Owner owner = buck.getOwner(); return (V) new S3FileStoreAttributeView(buck.getCreationDate(), buck.getName(), owner.getId(), owner.getDisplayName()); }
protected String[] getRow(BucketSummary summary) { String[] row = new String[3]; row[0] = summary.getBucket().getName(); row[1] = formatter.getCount(summary.getCount()); row[2] = formatter.getSize(summary.getSize()); return row; }
protected String[] getRow(BucketSummary summary) { String[] row = new String[3]; row[0] = summary.getBucket().getName(); row[1] = FormatUtils.getCount(summary.getCount()); row[2] = FormatUtils.getSize(summary.getSize()); return row; }