work_42_OSS?

1.OSS?

对象存储。

2.登录阿里云 控制台:

https://account.aliyun.com/login/qr_login.htm

3开启OSS服务

4.创建Bucket 列表(类似于一个大桶,也可以说是根目录)

work_42_OSS?

 5.root账户所有的权限是所有的aliyun权限,aliyun建议我们开启子用户AccessKey

work_42_OSS?

在控制台登录需要在RAM访问控制中找到子账户的登录地址

work_42_OSS?

 

 编程访问
启用 Accesskey ID和 Accesskey Secret,支持通过API或其他开发工具访问

 work_42_OSS?

 6.创建用户组,加入用户组,赋值OSS的权限。

work_42_OSS?

 7.生成子账户的Accesskey并记录

 8.Spring集成OSS

pom.xml

        <!--阿里云oss-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

application.yml

aliyun:
  #节点 无需http://或https:// 只需//后面的网址
  ENDPOINT: oss-cn-*******.aliyuncs.com
  #访问密钥ID
  ACCESSKEYID: ********************
  ACCESSKEYSECRET: ***********
  #bucket名
  BUCKETNAME: **************

重写OSS OssClient

/**
 * 功能描述:读取配置文件类
 *
 * @author:hdh
 * @date: 2021/2/5 10:39
 */
@Data
@Configuration
public class OssClientConfig {
    @Value("${aliyun.ENDPOINT}")
    public  String ENDPOINT;

    @Value("${aliyun.ACCESSKEYID}")
    public  String ACCESSKEYID;

    @Value("${aliyun.ACCESSKEYSECRET}")
    public  String ACCESSKEYSECRET;

    @Value("${aliyun.BUCKETNAME}")
    public  String BUCKETNAME;

    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
    }

    @Bean
    public String bucketName() {
        return BUCKETNAME;
    }
}

封装OSSutils

/**
 * 功能描述:oss封装工具类
 *
 * @author:hdh
 * @date: 2021/2/5 10:39
 */
@Component
public class OSSUtils {
    /**
     * oss客户端
     */
    @Autowired
    @Qualifier("ossClient")
    private OSS ossClient;


    @Autowired
    @Qualifier("bucketName")
    private String BUCKEN_NAME;

    /**
     * @param url        URL
     * @param objectName 上传文件目录和(包括文件名)例如“test/index.html”
     * @return void        返回类型
     * @throws
     * @Title: uploadByNetworkStream
     * @Description: 通过网络流上传文件
     */
    public void uploadByNetworkStream(URL url, String objectName) {
        try {
            InputStream inputStream = url.openStream();
            ossClient.putObject(BUCKEN_NAME, objectName, inputStream);
            ossClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * @param inputStream 输入流
     * @param objectName  上传文件目录和(包括文件名) 例如“test/a.jpg”
     * @return void        返回类型
     * @throws
     * @Title: uploadByInputStream
     * @Description: 通过输入流上传文件
     */
    public void uploadByInputStream(InputStream inputStream,
                                    String objectName) {
        ossClient.putObject(BUCKEN_NAME, objectName, inputStream);
    }

    /**
     * @param file       上传的文件
     * @param objectName 上传文件目录和(包括文件名) 例如“test/a.jpg”
     * @return void        返回类型
     * @throws
     * @Title: uploadByFile
     * @Description: 通过file上传文件
     */
    public PutObjectResult uploadByFile(File file, String objectName) {
        try {
            PutObjectResult putObjectResult = ossClient.putObject(BUCKEN_NAME, objectName, file);
            return putObjectResult;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }


    /**
     * @param key 文件路径/名称,例如“test/a.txt”
     * @return void            返回类型
     * @throws
     * @Title: deleteFile
     * @Description: 根据key删除oss服务器上的文件
     */
    public void deleteFile(String key) {
        ossClient.deleteObject(BUCKEN_NAME, key);
    }

    /**
     * @param key 文件路径和名称
     * @return InputStream    文件输入流
     * @throws
     * @Title: getInputStreamByOSS
     * @Description:根据key获取服务器上的文件的输入流
     */
    public InputStream getInputStreamByOSS(String key) {
        InputStream content = null;
        try {
            OSSObject ossObj = ossClient.getObject(BUCKEN_NAME, key);
            content = ossObj.getObjectContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * @param ossClient oss客户端
     * @return List<String>  文件路径和大小集合
     * @throws
     * @Title: queryAllObject
     * @Description: 查询某个bucket里面的所有文件
     */
    public List<String> queryAllObject(OSSClient ossClient) {
        List<String> results = new ArrayList<String>();
        try {
            // ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。
            ObjectListing objectListing = ossClient.listObjects(BUCKEN_NAME);
            // objectListing.getObjectSummaries获取所有文件的描述信息。
            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                results.add(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return results;
    }

    /**
     * @param fielURL:图片地址
     * @Description:
     * @Author: hdh
     * @Date: 2021/2/7
     * @return: null
     **/
    public String getOSSUrl(String fielURL) {

        // 设置URL过期时间为1小时
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
        GeneratePresignedUrlRequest generatePresignedUrlRequest;
        generatePresignedUrlRequest = new GeneratePresignedUrlRequest(BUCKEN_NAME, fielURL);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }
}
MultipartFileToFile文件流转换类
/**
 * @ClassName MultipartFileToFile
 * @Description MultipartFile转fie
 * @Author TongGuoBo
 * @Date 2019/6/19 13:48
 **/
public class MultipartFileToFile {
 
    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
 
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }
 
    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 删除本地临时文件
     * @param file
     */
    public static void delteTempFile(File file) {
    if (file != null) {
        File del = new File(file.toURI());
        del.delete();
    }
}
}

 

上一篇:php – 限制Woocommerce中购物车的数量


下一篇:php – 在Woocommerce中为每3件物品的统一运费添加额外费用