阿里OSS对象存储使用-阿里云开发者社区

开发者社区> 云计算> 正文
登录阅读全文

阿里OSS对象存储使用

简介: 数据统计 几个概念 Bucket:OSS 存储区域名 EndPoint:大区域名 Bucket 域名:bucket 域名 AccessKey ID:阿里云产品的通用id Access Key Secret:密钥 新建bucket 项目中使用OSS pom.
img_9e4790abb3d467f909fd27982f3b9df5.png
数据统计

几个概念

Bucket:OSS 存储区域名
EndPoint:大区域名
Bucket 域名:bucket 域名

AccessKey ID:阿里云产品的通用id
Access Key Secret:密钥

img_d8001d66aa3cb36afee6e54a3d499901.png
新建bucket

项目中使用OSS

pom.xml

 <!--aliyun OSS-->
    <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.3.0</version>
    </dependency>
工具类
/**
 * @Author:LiuPu
 * @Date:2018/11/8 16:02
 * @Description:
 * @Version 1.011
 */
public class OSSUtil {
    //EndPoint
    private static String endpoint = "https://oss-cn-xxxxxxx.aliyuncs.com";
    //AccessKey ID
    private static String accessKeyId = "xxxxxxxxx";
    //Access Key Secret
    private static String accessKeySecret = "xxxxxxxxxxxxxxxxxxxxxxx";
    //Bucket's name
    private static String bucketName = "xxxxxxxxx";
    //OSS Folder Name
    private static String folder = "xxxxx/";
    //Bucket's domain name
    private static String key = "https://xxxxxxxxx.oss-cn-xxxxxxxxx.aliyuncs.com/";

    /**
     * 流文件上传
     * @param fileName
     * @param inputStream
     * @return 图片的url
     */
    public static String uploadImageToOSS(String fileName, InputStream inputStream) {
        //创建OSS客户端
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            Date date = new Date();
            SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmm-");
            String imageTime = yyyyMMddHHmmss.format(date);
            String[] names = fileName.split("[.]");
            //图片名称:时间+UUID
            String name = imageTime+uuid + "." + names[names.length - 1];
            //putObject 上传对象
            ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStream));
            return key + folder + name;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        return null;
    }

    /**
     * 多图上传
     * @param fileNames
     * @param inputStreams
     * @return
     */
    public static String[] uploadImageToOSS(String[] fileNames, InputStream[] inputStreams) {
        //创建OSS客户端
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue(
                    Arrays.asList(fileNames));
            ConcurrentLinkedQueue<InputStream> streamConcurrentLinkedQueue = new ConcurrentLinkedQueue<>(
                    Arrays.asList(inputStreams));
            Iterator<InputStream> inputStreamss = streamConcurrentLinkedQueue.iterator();
            ConcurrentLinkedQueue c = new ConcurrentLinkedQueue();
            for (Iterator<String> iterator = concurrentLinkedQueue.iterator();
                 iterator.hasNext() && inputStreamss.hasNext(); ) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String[] names = iterator.next().split("[.]");
                String name = uuid + "." + names[names.length - 1];
                ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStreamss.next()));
                c.add(key + folder + name);
            }
            return (String[]) c.toArray(new String[0]);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        return null;
    }

    /**
     * 删除图片
     * @param url
     */
    public void deleteImg(String url) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        if (url == null || "".equals(url)) {
            return;
        }
        String[] paths = url.split("[.]");
        /**
         * 文件夹是否存在
         */
        if (!ossClient.doesObjectExist(bucketName, folder)) {
            ossClient.putObject(bucketName, folder, new ByteArrayInputStream(new byte[0]));
        }
        String[] name = paths[paths.length - 2].split("[/]");
        /**
         * 对象是否存在
         */
        if (ossClient
                .doesObjectExist(bucketName,
                        folder + name[name.length - 1] + "." + paths[paths.length - 1])) {
            /**
             * 删除存在对象
             */
            ossClient
                    .deleteObject(bucketName, folder + name[name.length - 1] + "." + paths[paths.length - 1]);
        }
        ossClient.shutdown();
    }

}

img_05f4e8b22e0f4019cf5e40de187e21cf.png
OSS控制台中预览文件

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章
最新文章
相关文章