Java连接使用Ceph(S3对象存储)代码示例

mtain 2020年07月19日 584次浏览

**注:**代码示例带源于网络

添加依赖

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.125</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
        </dependency>
    </dependencies>

工具类

package top.mtain;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;

import java.io.File;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.List;

public class AwsClient {
        static AmazonS3 s3;
        static TransferManager tx;
        private static String AWS_ACCESS_KEY = "VKXDEEGPDL84DVTMQ4V9";
        private static String AWS_SECRET_KEY = "QoYjW1C0TJt6GwF5hz05L1A6LYF9YUAlA4DOJOuc ";
    private static String HOST = "192.168.1.21:7480";

    static final String bucketName = "test03";

        static {

            ClientConfiguration clientConfig = new ClientConfiguration();

            clientConfig.setProtocol(Protocol.HTTP);

            s3=new AmazonS3Client(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY),clientConfig);
            Region usWest2 = Region.getRegion(Regions.CN_NORTH_1);
            s3.setRegion(usWest2);
            s3.setEndpoint(HOST);
        }

    public static Bucket createBucket(String bucketname) throws SocketTimeoutException {
        System.out.println("开始创建桶"+bucketname);
        Bucket bucket=null;
        bucket=s3.createBucket(bucketname);
        System.out.println(s3.toString());
        return  bucket;

    }

        /**  
        * @Title: uploadToS3  
        * @Description: 将文件上传至S3上并且返回url 
        * @param @param tempFile 目标文件 
        * @param @param remoteFileName 文件名 
        * @param @return 
        * @param @throws IOException    设定文件  
        * @return String    返回类型  
        * @throws  
        */  
        public static String uploadToS3(File tempFile, String remoteFileName) throws IOException {
            try {  
                //上传文件  
                s3.putObject(new PutObjectRequest(bucketName, remoteFileName, tempFile).withCannedAcl(CannedAccessControlList.PublicRead));
                //获取一个request  
                GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                        bucketName, remoteFileName);  
                //生成公用的url  
                URL url = s3.generatePresignedUrl(urlRequest);
                System.out.println("=========URL=================" + url + "============URL=============");  
                return url.toString();  
            } catch (AmazonServiceException ase) {
                ase.printStackTrace();  
            } catch (AmazonClientException ace) {
                ace.printStackTrace();  
            }  
            return null;  
        }  
      
         /**  
        * @Title: getContentFromS3  
        * @Description: 获取文件2进制流 
        * @param @param remoteFileName 
        * @param @throws IOException    设定文件  
        * @return S3ObjectInputStream    返回类型  数据流 
        * @throws  
        */  
        public static S3ObjectInputStream getContentFromS3(String remoteFileName) throws IOException {
            try {  
                GetObjectRequest request  = new GetObjectRequest(bucketName,remoteFileName);
                S3Object object = s3.getObject(request);  
                S3ObjectInputStream inputStream = object.getObjectContent();  
                return inputStream;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
            return null;  
         }  
          
          
        /**  
        * @Title: downFromS3  
        * @Description: 将文件下载到本地路径 
        * @param @param remoteFileName 文件名 
        * @param @param path 下载的路径 
        * @param @throws IOException    设定文件  
        * @return void    返回类型  
        * @throws  
        */  
        public static void downFromS3(String remoteFileName,String path) throws IOException {  
            try {  
                GetObjectRequest request  = new GetObjectRequest(bucketName,remoteFileName);  
                s3.getObject(request,new File(path));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
        }  
          
        /**  
        * @Title: getUrlFromS3  
        * @Description: 获取文件的url  
        * @param @param remoteFileName 文件名 
        * @param @return 
        * @param @throws IOException    设定文件  
        * @return String    返回类型  
        * @throws  
        */  
        public static String getUrlFromS3(String remoteFileName) throws IOException {  
            try {  
                GeneratePresignedUrlRequest httpRequest=new GeneratePresignedUrlRequest(bucketName, remoteFileName);  
                String url=s3.generatePresignedUrl(httpRequest).toString();//临时链接  
                return url;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
            return null;  
        }  
        /** 
         * 验证s3上是否存在名称为bucketName的Bucket 
         * @param s3 
         * @param bucketName 
         * @return 
         */  
        public static boolean checkBucketExists(AmazonS3 s3, String bucketName) {  
            List<Bucket> buckets = s3.listBuckets();
            for (Bucket bucket : buckets) {  
                if (bucketName.equals(bucket.getName())) {
                    return true;  
                }  
            }  
            return false;  
        }  
          
        public static void delFromS3(String remoteFileName) throws IOException {  
            try {  
                s3.deleteObject(bucketName, remoteFileName);  
            } catch (AmazonServiceException ase) {  
                ase.printStackTrace();  
            } catch (AmazonClientException ace) {  
                ace.printStackTrace();  
            }  
        }  
          
        public static void main(String[] args) throws Exception {  
            String key = "redisinfo";  
            File tempFile = new File("C:\\Users\\guosen\\Desktop\\redis.txt");  
            uploadToS3(tempFile,key);//上传文件  
            String url  = getUrlFromS3(key);//获取文件的url  
            System.out.println(url);  
    //      delFromS3(key);//删除文件  
        }
}

使用示例

        // 创建bucket
//        AwsClient.createBucket("test03");


        String key = "1.exe";

        // 上传文件
//        File tempFile = new File("D:\\1.exe");
//        AwsClient.uploadToS3(tempFile,key);


        // 获取文件的访问url
        // 每次生成新的url-一段时间后url会自动失效
        String url  = AwsClient.getUrlFromS3(key);
        System.out.println(url);

        // 文件下载到本地
        AwsClient.downFromS3("1.exe", "E:\\02.html");