Java SDK for Sirv S3

On this page

Easily upload and manage files on your Sirv account with the AWS SDK for Java. To help get you started, we created a client and common actions including upload, copy, move, delete, rename, create folder and list objects.

Connect to Sirv

1. Download the Sirv Java Samples (zip).

2. Unzip the file.

3. Open the samples in any IDE (e.g. IDEA, Eclipse) as a maven project and replace the references for YOUR_SIRV_S3_KEY, YOUR_SIRV_S3_SECRET and YOUR_SIRV_S3_BUCKET with the S3 credentials located on your account page, then run the samples:

import com.amazonaws.*;
import com.amazonaws.auth.*;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;

public class S3JavaSamples {

    public static void main(String[] args) {
        AWSCredentials credentials = new BasicAWSCredentials("YOUR_SIRV_S3_KEY", "YOUR_SIRV_S3_SECRET");
        String bucketName = "YOUR_SIRV_S3_BUCKET";
        String endpoint = "";

        // create client configuration
        ClientConfiguration clientConfig = new ClientConfiguration();

        // create S3 client
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                // consider usage of DefaultAWSCredentialsProviderChain class to avoid credentials hardcoding in the code
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                // any region can be used
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, Regions.DEFAULT_REGION.getName()))
        try {

        // Enter your commands here - see below for example commands

        } catch (AmazonServiceException ase) {
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Error Message: " + ace.getMessage());

4. Now use any of the code samples below to perform common actions.

List your S3 buckets

           System.out.println("Your buckets:");
           for (Bucket bucket : s3Client.listBuckets()) {
               System.out.printf(" - %s\n", bucket.getName());

Create folder

           String folderName = "Example-Folder/";
           System.out.printf("Creating folder %s inside S3 bucket %s...\n", folderName, bucketName);
           // Create a folder
           // Create metadata for your folder and set content-length to 0
           ObjectMetadata metadata = new ObjectMetadata();
           // Create empty content
           InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
           // Create a PutObjectRequest passing the folder name suffixed by /
           PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName, emptyContent, metadata);
           // Send request to S3 to create a folder
           System.out.printf("Folder %s was successfully created.\n", folderName, bucketName);

List folder contents with certain prefix

           String prefix = "Photos";
           System.out.printf("Objects in %s bucket with prefix %s :\n", bucketName, prefix);
           ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest().withBucketName(bucketName)
           for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
               if (!objectSummary.getKey().equals(prefix)) {
                   System.out.printf(" - %s  " + "(size = %s)\n", objectSummary.getKey(), objectSummary.getSize());

Upload file

           File fileToUpload = new File("PATH_TO_FILE_ON_LOCAL_FS");
           // File name on S3 bucket. For example: MyPhoto.jpg or /Photos/MyPhoto.jpg
           String fileNameOnS3 = "image.jpg";
           System.out.printf("Uploading file %s to S3 bucket %s... \n", fileToUpload.getPath(), bucketName);
           s3Client.putObject(bucketName, fileNameOnS3, fileToUpload);
           System.out.printf("File %s successfully uploaded to %s bucket.\n", fileNameOnS3, bucketName);

Rename file

           String fileToRename = "image.jpg";
           String newFileName = "image-with-new-name.jpg";
           System.out.printf("Renaming file %s to %s inside S3 bucket %s...\n", fileToRename, newFileName, bucketName);
           s3Client.copyObject(bucketName, fileToRename, bucketName, newFileName);
           s3Client.deleteObject(bucketName, fileToRename);
           System.out.println("File successfully renamed.");

Copy file

           String objectToCopy = "image.jpg";
           String newObjectName = "image_copy.jpg";
           System.out.printf("Copying file %s to %s inside S3 bucket %s...\n", objectToCopy, newObjectName, bucketName);
           s3Client.copyObject(bucketName, objectToCopy, bucketName, newObjectName);
           System.out.println("File successfully copied.");

Move file

           String fileToMove = "image.jpg";
           String newFilePath = "/Another-Folder/image.jpg";
           System.out.printf("Moving file %s to %s inside S3 bucket %s...\n", fileToMove, newFilePath, bucketName);
           s3Client.copyObject(bucketName, fileToMove, bucketName, newFilePath);
           s3Client.deleteObject(bucketName, fileToMove);
           System.out.println("File successfully moved.");

Delete file

           String fileToRemove = "image.jpg";
           System.out.printf("Removing file %s from S3 bucket %s...\n", fileToRemove, bucketName);
           s3Client.deleteObject(bucketName, fileToRemove);
           System.out.printf("File %s successfully removed.\n", fileToRemove);

Was this article helpful?

Get help from a Sirv expert