org.openflamingo.fs.s3.S3Utils.java Source code

Java tutorial

Introduction

Here is the source code for org.openflamingo.fs.s3.S3Utils.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.openflamingo.fs.s3;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import org.openflamingo.model.rest.FileInfo;
import org.openflamingo.util.DateUtils;
import org.openflamingo.util.FileUtils;
import org.openflamingo.util.StringUtils;

import java.util.*;

/**
 * Amazon S3 Utility.
 *
 * @author Byoung Gon, Kim
 * @since 0.3
 */
public class S3Utils {

    /**
     * Bucket ? Object Key ? Fully Qualified Path ? ?.
     *
     * @param path Fully Qualified Path
     * @return ?  <tt>true</tt>
     */
    public static boolean isDirectory(String path) {
        return path.endsWith("/");
    }

    /**
     * Fully Qualified Path? Bucket?  .
     *
     * @param path Fully Qualified Path
     * @return Bucket
     */
    public static String getBucket(String path) {
        if ("/".equals(path))
            return "";
        String pathUnits[] = org.apache.commons.lang.StringUtils.split(path, "/");
        return pathUnits[0];
    }

    /**
     * S3 Object Key . <b>  Bucket Directory? Object Key ? ? .</b>
     *
     * @param path Path
     * @return Object Key
     */
    public static String getObjectKey(String path) {
        String pathUnits[] = org.apache.commons.lang.StringUtils.split(path, "/");
        StringBuffer relativePath = new StringBuffer();

        for (int i = 1; i < pathUnits.length; i++) {
            relativePath.append(pathUnits[i] + "/");
        }
        return relativePath.toString();
    }

    /**
     * Bucket  .
     *
     * @param client     Amazon S3 Client
     * @param bucketName Bucket Name
     */
    public static Map<String, String> getBucketInfo(AmazonS3Client client, String bucketName) {
        Bucket bucket = getBucket(client, bucketName);
        if (bucket == null) {
            return null;
        }

        ObjectMetadata objectMetadata = client.getObjectMetadata(bucketName, "");

        Map<String, String> map = new HashMap<String, String>();
        map.put("name", bucket.getName());
        map.put("ownerName", bucket.getOwner().getDisplayName());
        map.put("ownerId", bucket.getOwner().getId());
        setValue("create", bucket.getCreationDate(), map);
        setValue("location", client.getBucketLocation(bucketName), map);
        setValue("version", objectMetadata.getVersionId(), map);
        setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
        setValue("contentType", objectMetadata.getContentType(), map);
        setValue("etag", objectMetadata.getETag(), map);
        setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
        setValue("contentLength", objectMetadata.getContentLength(), map);
        setValue("lastModified", objectMetadata.getLastModified(), map);

        return map;
    }

    /**
     * Object  .
     *
     * @param client     Amazon S3 Client
     * @param bucketName Bucket Name
     */
    public static Map<String, String> getObject(AmazonS3Client client, String bucketName, String objectKey) {
        S3Object object = client.getObject(bucketName, objectKey);
        ObjectMetadata objectMetadata = object.getObjectMetadata();

        Map<String, String> map = new HashMap<String, String>();

        if (!object.getKey().endsWith("/")) {
            String qualifiedPath = "/" + bucketName + "/" + object.getKey();
            map.put("bucketName", object.getBucketName());
            map.put("name", FileUtils.getFilename(qualifiedPath));
            map.put("path", qualifiedPath);
        } else {
            map.put("bucketName", object.getBucketName());
            map.put("name", object.getKey());
            map.put("name", "/" + bucketName + "/" + object.getKey());
        }

        setValue("redirectionLocation", object.getRedirectLocation(), map);
        setValue("version", objectMetadata.getVersionId(), map);
        setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
        setValue("contentType", objectMetadata.getContentType(), map);
        setValue("etag", objectMetadata.getETag(), map);
        setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
        setValue("contentLength", objectMetadata.getContentLength(), map);
        setValue("lastModified", objectMetadata.getLastModified(), map);
        return map;
    }

    /**
     * Object  .
     *
     * @param client     Amazon S3 Client
     * @param bucketName Bucket Name
     */
    public static Map<String, String> getDirectory(AmazonS3Client client, String bucketName, String objectKey) {
        S3Object object = client.getObject(bucketName, objectKey);
        ObjectMetadata objectMetadata = object.getObjectMetadata();

        List<FileInfo> filesList = new ArrayList<FileInfo>();
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(object.getBucketName())
                .withPrefix(objectKey).withDelimiter("/");

        ObjectListing objectListing = null;

        do {
            objectListing = client.listObjects(listObjectsRequest);
            List<String> commonPrefixes = objectListing.getCommonPrefixes();
            List<S3ObjectSummary> summary = objectListing.getObjectSummaries();
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());

        Map<String, String> map = new HashMap<String, String>();

        map.put("bucketName", object.getBucketName());
        map.put("name", object.getKey());
        map.put("redirectionLocation", object.getRedirectLocation());

        setValue("version", objectMetadata.getVersionId(), map);
        setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
        setValue("contentType", objectMetadata.getContentType(), map);
        setValue("etag", objectMetadata.getETag(), map);
        setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
        setValue("contentLength", objectMetadata.getContentLength(), map);
        setValue("lastModified", objectMetadata.getLastModified(), map);
        return null;
    }

    /**
     *   NULL  ? ? .
     *
     * @param key   Map?  Key
     * @param value Map?  Key? 
     * @param map   Map
     */
    public static void setValue(String key, Object value, Map<String, String> map) {
        if (value == null)
            return;
        if (value instanceof String) {
            String msg = (String) value;
            if (!StringUtils.isEmpty(msg))
                map.put(key, msg);
        }

        if (value instanceof Date) {
            Date d = (Date) value;
            map.put(key, DateUtils.parseDate(d, "yyyy-MM-dd HH:mm:ss"));
        }

        if (value instanceof Number) {
            map.put(key, "" + value);
        }
    }

    /**
     * Bucket  .
     *
     * @param client     Amazon S3 Client
     * @param bucketName Bucket Name
     * @return Bucket?   Bucket ,  ?  <tt>null</tt>? 
     */
    public static Bucket getBucket(AmazonS3Client client, String bucketName) {
        List<Bucket> buckets = client.listBuckets();
        for (Bucket bucket : buckets) {
            if (bucketName.equals(bucket.getName())) {
                return bucket;
            }
        }
        return null;
    }

    /**
     *  Region? Amazon S3 Client ?.
     *
     * @param region    Amazon S3 Region
     * @param accessKey Amazon S3 Access Key
     * @param secretKey Amazon S3 Secret Key
     * @return Amazon S3 Client
     */
    public static AmazonS3Client getAmazonS3Client(String region, String accessKey, String secretKey) {
        Region awsRegion = Region.getRegion(Regions.valueOf(region));
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonS3Client awsClient = new AmazonS3Client(awsCredentials);
        awsClient.setRegion(awsRegion);
        return awsClient;
    }
}