其他分享
首页 > 其他分享> > 阿里云OSS对象存储的简单使用

阿里云OSS对象存储的简单使用

作者:互联网

文章目录

简单概念

配置文件

oss:
  endpoint: 大区域名   # oss-cn-xxxxxx.aliyuncs.com
  keyid:    阿里云产品的通用id
  keysecret: 密钥     # 填写生成的Accesssecret
  bucketname: bucket名称
  filehost: folder    # bucket下的文件夹路径

OSS bean

@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class OSSConstant {
    private  String endpoint;
    private  String keyid;
    private  String keysecret;
    private  String bucketname;
    private  String filehost;
}

pom.xml

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.3.0</version>
</dependency>

工具类

@Component
@Slf4j
public class AliyunOSSUtil {
    @Autowired
    private OSSConstant ossConstant;

    private OSSClient Client;

    private String bucketName;
    private String folder;
    private final String prefix = "https://xxxxxxxxx.oss-cn-xxxxxxx.aliyuncs.com/";

    /**
     * 初始化客户端
     * @return
     */
    private OSSClient getOssClient(){
        if(Client == null){
            Client = new OSSClient(ossConstant.getEndpoint(), ossConstant.getKeyid(), ossConstant.getKeysecret());
            bucketName = ossConstant.getBucketname();
            folder = ossConstant.getFilehost();
        }
        return Client;
    }

    /**
     * 多图上传
     * @return
     */
    public String[] uploadImageToOSS(String[] fileNames, InputStream[] inputStreams) {
        //创建OSS客户端
        OSSClient ossClient = getOssClient();
        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( prefix + 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 = getOssClient();
        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();
    }

}

标签:存储,String,OSS,ossClient,private,阿里,new,folder,name
来源: https://blog.csdn.net/JiangHxin/article/details/114605747