md.djembe.aws.AmazonS3WebClient.java Source code

Java tutorial

Introduction

Here is the source code for md.djembe.aws.AmazonS3WebClient.java

Source

/*
 * Copyright 2015 Djembe.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package md.djembe.aws;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.util.StringUtils;
import md.djembe.cache.ApplicationCache;
import md.djembe.enums.WebAppProperties;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

public class AmazonS3WebClient {

    private static final Logger LOGGER = Logger.getLogger(AmazonS3WebClient.class);

    private static String ACCESS_KEY;
    private static String SECRET_KEY;
    private static String BUCKET_NAME;

    public static AmazonS3 getS3Client() {
        ACCESS_KEY = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_ACCESS_KEY);
        SECRET_KEY = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_SECRET_KEY);
        BUCKET_NAME = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_BUCKET_NAME);

        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        AmazonS3 amazonS3 = new AmazonS3Client(awsCredentials);
        return amazonS3;
    }

    public static void uploadToBucket(final String filename, final File image) {

        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, filename, image);
        putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        if (putObjectRequest.getMetadata() == null) {
            putObjectRequest.setMetadata(new ObjectMetadata());
        }
        putObjectRequest.getMetadata().setContentType("image/jpeg");

        AmazonS3 s3Client = getS3Client();
        s3Client.putObject(putObjectRequest);
        LOGGER.info("File Uploaded to Amazon S3.");
    }

    public static void listBucket() {
        AmazonS3 s3Client = getS3Client();
        List<Bucket> buckets = s3Client.listBuckets();
        for (Bucket bucket : buckets) {
            LOGGER.info("bucket : " + bucket.getName() + "\t" + StringUtils.fromDate(bucket.getCreationDate()));
        }
    }

}