Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.upload; import com.sfs.Formatter; import java.io.File; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.jets3t.service.Jets3tProperties; import org.jets3t.service.S3Service; import org.jets3t.service.S3ServiceException; import org.jets3t.service.acl.AccessControlList; import org.jets3t.service.acl.GroupGrantee; import org.jets3t.service.acl.Permission; import org.jets3t.service.impl.rest.httpclient.RestS3Service; import org.jets3t.service.model.S3Bucket; import org.jets3t.service.model.S3Object; import org.jets3t.service.security.AWSCredentials; /** * The Class S3FileUploadImpl. * * @author David Harrison */ public class S3FileUploadImpl implements FileUpload { /** The logger. */ private static Logger logger = Logger.getLogger(S3FileUploadImpl.class); /** The aws access key. */ private String awsAccessKey; /** The aws secret key. */ private String awsSecretKey; /** The aws bucket name. */ private String awsBucketName; /** The aws url. */ private String awsUrl = "http://s3.amazonaws.com/"; /** * Sets the aws access key. * * @param awsAccessKeyVal the new aws access key */ public final void setAwsAccessKey(final String awsAccessKeyVal) { this.awsAccessKey = awsAccessKeyVal; } /** * Sets the aws secret key. * * @param awsSecretKeyVal the new aws secret key */ public final void setAwsSecretKey(final String awsSecretKeyVal) { this.awsSecretKey = awsSecretKeyVal; } /** * Sets the aws bucket name. * * @param awsBucketNameVal the new aws bucket name */ public final void setAwsBucketName(final String awsBucketNameVal) { this.awsBucketName = awsBucketNameVal; } /** * Upload the identified File to the defined storage pool. * * @param file the file * * @return the file upload details * * @throws FileUploadException the file upload exception */ public final FileUploadDetails upload(final File file) throws FileUploadException { if (file == null) { throw new FileUploadException("The File object cannot be null"); } if (!file.exists()) { throw new FileUploadException("No file exists to upload"); } if (StringUtils.isBlank(this.awsAccessKey)) { throw new FileUploadException("An AWS access key is required"); } if (StringUtils.isBlank(this.awsSecretKey)) { throw new FileUploadException("An AWS secret key is required"); } if (StringUtils.isBlank(this.awsBucketName)) { throw new FileUploadException("An AWS bucket name is required"); } FileUploadDetails details = null; final Date currentDate = Calendar.getInstance().getTime(); // Add a prefix to ensure that there isn't a duplicate file name issue. final String format = "yyyyMMddhhmm_"; final String fileName = Formatter.numericDate(currentDate, format) + file.getName(); try { AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey.trim(), awsSecretKey.trim()); Jets3tProperties jp = new Jets3tProperties(); String proxyHost = System.getProperty("https.proxyHost"); String proxyPortString = System.getProperty("https.proxyPort"); int proxyPort = -1; try { proxyPort = Integer.parseInt(proxyPortString); } catch (NumberFormatException nfe) { logger.debug("No proxy port defined", nfe); proxyPort = -1; } if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) { jp.setProperty("httpclient.proxy-host", proxyHost); jp.setProperty("httpclient.proxy-port", String.valueOf(proxyPort)); jp.setProperty("httpclient.proxy-autodetect", "false"); jp.setProperty("s3service.https-only", "true"); } S3Service s3Service = new RestS3Service(awsCredentials, null, null, jp); S3Bucket[] myBuckets = s3Service.listAllBuckets(); logger.info("Buckets in S3 account: " + myBuckets.length); S3Bucket uploadBucket = s3Service.getOrCreateBucket(awsBucketName.trim()); AccessControlList acl = new AccessControlList(); acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ); acl.setOwner(uploadBucket.getOwner()); S3Object fileObject = new S3Object(uploadBucket, file); fileObject.setKey(fileName); fileObject.setAcl(acl); s3Service.putObject(uploadBucket, fileObject); final String url = this.awsUrl + uploadBucket.getName() + "/" + fileObject.getKey(); logger.info("View public object contents here: " + url); details = new FileUploadDetails(); details.setFileName(file.getName()); details.setFileSize(file.length()); details.setUrl(url); } catch (S3ServiceException s3se) { throw new FileUploadException("Error performing S3 upload: " + s3se.getMessage()); } catch (NoSuchAlgorithmException nsae) { throw new FileUploadException("Error encrypting file for upload: " + nsae.getMessage()); } catch (IOException ioe) { throw new FileUploadException("Error accessing file to upload: " + ioe.getMessage()); } return details; } }