Search in sources :

Example 31 with S3Client

use of org.jclouds.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method listBucketObjects.

// Returns the names of all images in the given bucket.
public List<String> listBucketObjects(String bucketName) {
    S3Client s3 = getClient();
    String keyName;
    List<String> keys = new ArrayList<>();
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            keyName = myValue.key();
            keys.add(keyName);
        }
        return keys;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 32 with S3Client

use of org.jclouds.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method tagAssets.

// tag assets with labels in the given list.
public void tagAssets(List myList, String bucketName) {
    try {
        S3Client s3 = getClient();
        int len = myList.size();
        String assetName = "";
        String labelName = "";
        String labelValue = "";
        // tag all the assets in the list.
        for (Object o : myList) {
            // Need to get the WorkItem from each list.
            List innerList = (List) o;
            for (Object value : innerList) {
                WorkItem workItem = (WorkItem) value;
                assetName = workItem.getKey();
                labelName = workItem.getName();
                labelValue = workItem.getConfidence();
                tagExistingObject(s3, bucketName, assetName, labelName, labelValue);
            }
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ArrayList(java.util.ArrayList) List(java.util.List) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 33 with S3Client

use of org.jclouds.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class IAMScenario method assumeGivenRole.

// Invoke an Amazon S3 operation using the Assumed Role.
public static void assumeGivenRole(String roleArn, String roleSessionName, String bucketName) {
    StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();
    try {
        AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
        AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
        Credentials myCreds = roleResponse.credentials();
        String key = myCreds.accessKeyId();
        String secKey = myCreds.secretAccessKey();
        String secToken = myCreds.sessionToken();
        // List all objects in an Amazon S3 bucket using the temp creds.
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(key, secKey, secToken))).region(region).build();
        System.out.println("Created a S3Client using temp credentials.");
        System.out.println("Listing objects in " + bucketName);
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            System.out.println("The name of the key is " + myValue.key());
            System.out.println("The owner is " + myValue.owner());
        }
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) AssumeRoleRequest(software.amazon.awssdk.services.sts.model.AssumeRoleRequest) ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) StsException(software.amazon.awssdk.services.sts.model.StsException) AssumeRoleResponse(software.amazon.awssdk.services.sts.model.AssumeRoleResponse) Region(software.amazon.awssdk.regions.Region) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) Credentials(software.amazon.awssdk.services.sts.model.Credentials)

Example 34 with S3Client

use of org.jclouds.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class VPCS3Example method main.

public static void main(String[] args) throws URISyntaxException {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <vpcBucketURL> \n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket from which objects are read. \n\n" + "    vpcBucketURL - the URL of the bucket located in your virtual private cloud (VPC) (for example,  https://bucket.vpce-xxxxxc4d-5e6f.s3.us-east-1.vpce.amazonaws.com";
    ;
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String vpcBucketURL = args[1];
    URI myURI = new URI(vpcBucketURL);
    Region region = Region.US_EAST_1;
    S3Client s3 = S3Client.builder().endpointOverride(myURI).region(region).build();
    listBucketObjects(s3, bucketName);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client) URI(java.net.URI)

Example 35 with S3Client

use of org.jclouds.s3.S3Client in project aws-doc-sdk-examples by awsdocs.

the class S3Service method getObjectBytes.

public byte[] getObjectBytes(String bucketName, String keyName) {
    try {
        S3Client s3 = getClient();
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Client(software.amazon.awssdk.services.s3.S3Client) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Aggregations

S3Client (software.amazon.awssdk.services.s3.S3Client)63 Region (software.amazon.awssdk.regions.Region)43 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)8 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 List (java.util.List)5 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)5 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)5 SupplierEx (com.hazelcast.function.SupplierEx)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)3 S3ClientBuilder (software.amazon.awssdk.services.s3.S3ClientBuilder)3 Pipeline (com.hazelcast.jet.pipeline.Pipeline)2 TestSources (com.hazelcast.jet.pipeline.test.TestSources)2 ILogger (com.hazelcast.logging.ILogger)2 Logger (com.hazelcast.logging.Logger)2 HazelcastSerialClassRunner (com.hazelcast.test.HazelcastSerialClassRunner)2 Collectors (java.util.stream.Collectors)2 AfterClass (org.junit.AfterClass)2 Test (org.junit.Test)2