fr.xebia.cocktail.AmazonS3FileStorageService.java Source code

Java tutorial

Introduction

Here is the source code for fr.xebia.cocktail.AmazonS3FileStorageService.java

Source

/*
 * Copyright 2008-2012 Xebia and the original author or authors.
 *
 * 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 fr.xebia.cocktail;

import java.io.InputStream;
import java.util.Map;
import java.util.Random;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

/**
 * Amazon AWS S3 storage for images.
 * 
 * @author <a href="mailto:cleclerc@xebia.fr">Cyrille Le Clerc</a>
 */
@Service
public class AmazonS3FileStorageService {

    private Map<String, String> contentTypeByFileExtension;

    private Map<String, String> defaultFileExtensionByContentType;

    private AmazonS3 amazonS3;

    @Value("${aws_s3_bucket_base_url}")
    private String amazonS3BucketBaseUrl;

    @Value("${aws_s3_bucket_name}")
    private String amazonS3BucketName;

    private final Random random = new Random();

    @Inject
    public AmazonS3FileStorageService(@Value("${aws_access_key}") String awsAccessKey,
            @Value("${aws_secret_key}") String awsSecretKey) {

        amazonS3 = new AmazonS3Client(new BasicAWSCredentials(awsAccessKey, awsSecretKey));

        contentTypeByFileExtension = Maps.newHashMapWithExpectedSize(4);
        contentTypeByFileExtension.put("jpg", "image/jpeg");
        contentTypeByFileExtension.put("jpeg", "image/jpeg");
        contentTypeByFileExtension.put("png", "image/png");
        contentTypeByFileExtension.put("gif", "image/gif");

        defaultFileExtensionByContentType = Maps.newHashMapWithExpectedSize(3);
        defaultFileExtensionByContentType.put("image/jpeg", "jpg");
        defaultFileExtensionByContentType.put("image/png", "png");
        defaultFileExtensionByContentType.put("image/gif", "gif");
    }

    @Nullable
    public String findContentType(String fileName) {
        String fileExtension = Iterables.getLast(Splitter.on('.').split(fileName), null);
        fileExtension = Strings.nullToEmpty(fileExtension).toLowerCase();
        return contentTypeByFileExtension.get(fileExtension);
    }

    @Nonnull
    public String storeFile(InputStream in, ObjectMetadata objectMetadata) {
        String extension = defaultFileExtensionByContentType.get(objectMetadata.getContentType());
        String fileName = Math.abs(random.nextLong()) + "." + extension;

        amazonS3.putObject(amazonS3BucketName, fileName, in, objectMetadata);

        return amazonS3BucketBaseUrl + fileName;
    }
}