文件存储到amazon S3

    技术2024-04-13  7

    由于云技术的日益成熟,越来越多的公司存储文件时会用到云技术,而亚马逊就提供了一个日益成熟的云环境的服务器群方便存储的文件,我就以简单存贮图片到amazon S3为例,简单调用了一下amazon提供的JAR包(aws-java-sdk-1.1.1.jar)的几个API,本文的另一个着重点在于上传时会对图片的格式(例如18x18),大小(例如512K)和类型(例如JPEG)有要求,符合要求的则上传成功,否则失败!

     

    自己提供一个接口service

    import java.io.File; /** * Provides an interface for accessing the storage server. * * @author andy.gao */ public interface AvatarStorageService { /** * Uploads a new object to the specified server * * @param file The file containing the data to be uploaded to specified server * @param key The file key. * @return The value <code>true</code> if upload file to server successful, otherwise <code>false</code>. */ public boolean putObject(File file, String key); /** * Deletes the specified object in the specified server. * @param key The key of the object to delete. * @return The value <code>true</code> if delete file to server successful, otherwise <code>false</code>. */ public boolean deleteObject(String key); }

     

    AvatarStorageService的实现类AvatarS3StorageServiceImpl

    调用此JAR包的三个API:

                  1. void com.amazonaws.services.s3.AmazonS3.deleteObject(String bucketName, String key) ---此API删除文件:buckName, 文件夹名;key,所删除文件的key值                  

                  2. PutObjectResult com.amazonaws.services.s3.AmazonS3.putObject(String bucketName, String key, File file)    ----此API上传文件                      

                  3.void com.amazonaws.services.s3.AmazonS3.setObjectAcl(String bucketName, String key, CannedAccessControlList acl) -----此API设置访问此文件的权限(private,publicRead,PublicReadWrite, AuthenticatedRead等,详细可看API)

    import java.io.File; import java.util.Map; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.ea.online.ebisu.avatar.avatar.AvatarStorageService; import com.ea.online.ebisu.avatar.utils.Constant; import com.ea.rs4.Logger; import com.ea.rs4.di.Named; import com.ea.rs4.utils.StringUtils; /** * Provides the client for accessing the Amazon S3 web service. * * @author andy.gao */ public class AvatarS3StorageServiceImpl implements AvatarStorageService { private static final Logger logger = new Logger(AvatarS3StorageServiceImpl.class.getName()); AmazonS3 s3client; String bucketName; private static final String BUCKET_NAME = "bucketName"; private static final String ACCESS_KEY = "accessKey"; private static final String SECRET_KEY = "secretKey"; private static final String MAX_ERROR_RETRY = "maxErrorRetry"; private static final String PROXY_HOST = "proxyHost"; private static final String PROXY_PORT = "proxyPort"; private static final String PROXY_USERNAME = "proxyUsername"; private static final String PROXY_PASSWORD = "proxyPassword"; public void setS3Client(AmazonS3Client s3Client){ this.s3client = s3Client; } public AvatarS3StorageServiceImpl(@Named("rs4.env.properties") Map<String, String> props) { bucketName = props.get(BUCKET_NAME); s3client = new AmazonS3Client(new BasicAWSCredentials(props.get(ACCESS_KEY), props.get(SECRET_KEY)), initClientConfig(props)); } /* * Initial client configuration options such as proxy settings, max retry attempts, etc. */ private ClientConfiguration initClientConfig(Map<String, String> props){ ClientConfiguration config = new ClientConfiguration(); int maxErrorRetry = props.get(MAX_ERROR_RETRY) == null ? 0 : Integer.parseInt(props.get(MAX_ERROR_RETRY)); config.withMaxErrorRetry(maxErrorRetry); String proxyHost = props.get(PROXY_HOST); if (StringUtils.hasValue(proxyHost)) { config.withProxyHost(proxyHost); config.withProxyPort(Integer.parseInt(props.get(PROXY_PORT))); config.withProxyUsername(props.get(PROXY_USERNAME)); config.withProxyPassword(props.get(PROXY_PASSWORD)); } return config; } /* * (non-Javadoc) * * @see com.ea.online.ebisu.avatar.avatar.AvatarStorageService#deleteObject(java.lang.String) */ @Override public boolean deleteObject(String key) { try { s3client.deleteObject(bucketName, key); } catch (Exception e) { logger.error("Delete object from Amazon S3 error, bucketName=%s; key=%s; errorCode=%s", e, bucketName, key, Constant.ERROR_CODE_AVATAR_DELETE_ERROR); return false; } return true; } /* * (non-Javadoc) * * @see com.ea.online.ebisu.avatar.avatar.AvatarStorageService#putObject(java.io.File) */ @Override public boolean putObject(File file, String key) { try { s3client.putObject(bucketName, key, file); s3client.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead); } catch (Exception e) { logger.error("Put object to Amazon S3 error, bucketName=%s;key=%s; errorCode=%s", e, bucketName, key, Constant.ERROR_CODE_AVATAR_UPLOAD_ERROR); return false; } finally { if (file != null) { file.delete(); } } return true; } }

    下面的主要是对图片文件上传时对格式,大小和类型的验证:

    String stream = HttpUtils.inputStreamToString(inputStream); file = createImageFile(stream); String dimension = getAvatarDimention(file); String imageType = CommonUtils.getImageFormatName(file); File uploadfile = new File(file.getPath().substring(0, file.getPath().lastIndexOf('.') + 1) + imageType); file.renameTo(uploadfile);

    /** * covnert stream to string. */ public static String inputStreamToString(InputStream result) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = result.read(b)) != -1;) { out.append(new String(b, 0, n)); } return out.toString(); }

    /** * Convert byte stream base on base64 format to temporary file */ public static File convertToTemporaryFileFromBase64(String stream) throws IOException { byte[] bytes = stream.getBytes("ASCII"); byte[] decoded = Base64.decodeBase64(bytes); File file = File.createTempFile(UUID.randomUUID().toString(), null); File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } file.createNewFile(); OutputStream os = new FileOutputStream(file); try { ByteArrayInputStream buffer = new ByteArrayInputStream(decoded); byte[] barr = new byte[1024]; while(true) { int r = buffer.read(barr); if(r <= 0) { break; } os.write(barr, 0, r); } } finally { os.flush(); os.close(); } return file; }

    private String getAvatarDimention(File file) throws BusinessException { String dimension; try { // Create template file dimension = CommonUtils.getImageDimension(file); String imageType = CommonUtils.getImageFormatName(file); Integer fileSize = CommonUtils.getFileSize(file); checkImageFile(imageType, dimension, fileSize); } catch (IOException e) { throw new BusinessException(e, null, Constant.ERROR_CODE_AVATAR_TYPE_NOT_SUPPORTED); } return dimension; }

    /** * Get image dimension * * @param imageFile image file * @return image dimension * @throws IOException */ public static String getImageDimension(File imageFile) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(imageFile); BufferedImage sourceImg = ImageIO.read(fis); if(sourceImg == null) { throw new IOException("image format error"); } String dimension = sourceImg.getWidth() + "x" + sourceImg.getHeight(); sourceImg.flush(); return dimension; } catch (RuntimeException e) { throw new IOException("image format error", e); } finally { if(fis != null) { fis.close(); } } }

    /** * Get image file format type * * @param imageFile image file * @return image type * @throws IOException */ public static String getImageFormatName(File imageFile) throws IOException { ImageInputStream iis = null; try { iis= ImageIO.createImageInputStream(imageFile); // Find all image readers that recognize the image format Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if(iter == null || !iter.hasNext()) { throw new IOException("image format error"); } ImageReader reader = iter.next(); // Return the format name String formatName = reader.getFormatName(); return formatName == null?null:formatName.toUpperCase(); } catch (RuntimeException e) { throw new IOException("image format error", e); } finally { if(iis != null) { iis.flush(); iis.close(); } } }

    /** * Get file size * * @param File file * @return file size * @throws IOException */ public static Integer getFileSize(File file) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(file); return fis.available(); } finally { if(fis != null) { fis.close(); } } }

    (注意:代码节选自自己的做的项目,意思了解即可)

    最新回复(0)