天道不一定酬所有勤
但是,天道只酬勤
Hollis出品的全套Java面试宝典不来了解一下吗?

阿里云OSS使用说明及部分代码

Hollis出品的全套Java面试宝典不来了解一下吗?

OSS是对象存储服务,地址:http://www.aliyun.com/product/oss/
个人用户要想使用OSS请先在阿里云购买!!!
开发文档:https://docs.aliyun.com/?spm=5176.383663.9.2.yKw6dv#/pub/oss/api-reference/abstract

文件上传

public String uploadFile(String key,File partFile) throws IOException{
        // 创建OSS客户端
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        return multipartUpload(key,partFile,client,bucketName);
    }

    /**
     *  /**
     * @param key 指明文件名+扩展名
     * @param partFile 要上传的文件
     * @return 上传后的文件名
     * @throws IOException
     */
    private String multipartUpload(String key,File partFile,OSSClient client,String bucketName) throws IOException{
        // 开始Multipart Upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
        final int partSize = 1024 * 1024 * 5;
        // 计算分块数目
        int partCount = (int) (partFile.length() / partSize);
        if (partFile.length() % partSize != 0){
            partCount++;
        }
        // 新建一个List保存每个分块上传后的ETag和PartNumber
        List<PartETag> partETags = new ArrayList<PartETag>();
        for(int i = 0; i < partCount; i++){
            // 获取文件流
            FileInputStream fis = new FileInputStream(partFile);
            // 跳到每个分块的开头
            long skipBytes = partSize * i;
            fis.skip(skipBytes);
            // 计算每个分块的大小
            long size = partSize < partFile.length() - skipBytes ?
                    partSize : partFile.length() - skipBytes;
            // 创建UploadPartRequest,上传分块
            UploadPartRequest uploadPartRequest = new UploadPartRequest();
            uploadPartRequest.setBucketName(bucketName);
            uploadPartRequest.setKey(key);
            uploadPartRequest.setUploadId(initiateMultipartUploadResult.getUploadId());
            uploadPartRequest.setInputStream(fis);
            uploadPartRequest.setPartSize(size);
            uploadPartRequest.setPartNumber(i + 1);
            UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
            // 将返回的PartETag保存到List中。
            partETags.add(uploadPartResult.getPartETag());
            // 关闭文件
            fis.close();
        }
        CompleteMultipartUploadRequest completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(bucketName,key, initiateMultipartUploadResult.getUploadId(), partETags);
        // 完成分块上传
        CompleteMultipartUploadResult completeMultipartUploadResult =
                client.completeMultipartUpload(completeMultipartUploadRequest);
        // 获得location

        return completeMultipartUploadResult.getKey();
    }

判断是否上传成功

    public boolean isUploadSuccess(String key){
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        ObjectMetadata objectMetadata = null;
        try{
            objectMetadata = client.getObjectMetadata(bucketName, key);
        }catch (OSSException e){
            //no suchKey
        }
        return objectMetadata!=null;
    }

读取所有文件

    public ObjectListing getAllObject(){
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        // 构造ListObjectsRequest请求
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        // List Objects
        return client.listObjects(listObjectsRequest);
    }

读取指定文件

    public OSSObject getObject(String key) throws OSSException{
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        // 获取Object,返回结果为OSSObject对象
        return client.getObject(bucketName, key);
    }

删除文件

    public void deleteObject(String key) throws OSSException {
        // 初始化OSSClient
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);

        // 删除Object
        client.deleteObject(bucketName, key);
    }

PS:使用OSS存储文件时,文件的Key最好就是文件名,并且带有扩展名,这样可以直接下载并且保存成对应类型的文件。文件的下载请参见:webx3文件下载

赞(0)
如未加特殊说明,此网站文章均为原创,转载必须注明出处。HollisChuang's Blog » 阿里云OSS使用说明及部分代码
Hollis出品的全套Java面试宝典不来了解一下吗?

评论 抢沙发

HollisChuang's Blog

联系我关于我