中国算力平台算力登记系统2.0
yanzhaofeige
2024-09-30 3c4fee1db116c11d4f04727cfe076d7c94daeaf2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.odcc.cpzidc.common.utils;
 
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
 
 
public class AliYunOss {
    private static String schema = "https://";
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
    private static String accessKeyId = "LTAI5tH1ASbFSFuvbZUgq7Ws";
    private static String accessKeySecret = "o0O1Ssf0cEmHGLs2SAxQjV0R2hJKvh";
    private static String bucketName = "all-in-one-files";
 
    public static String upload(File file,String fileName) throws FileNotFoundException {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(schema + endpoint, accessKeyId, accessKeySecret);
        //上传文件,删除文件
        System.out.println("ossClient = " + ossClient);
        // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
        InputStream inputStream = new FileInputStream(file);
 
        ossClient.putObject(bucketName,
                //key:表示文件保存在桶内的 路径+文件名
                fileName, inputStream);
 
        // 关闭OSSClient。
        ossClient.shutdown();
 
        //获取上传成功的文件地址
        //sh210325-guli.oss-cn-shanghai.aliyuncs.com/avatar/2021/07/30/13d5ce88785c4876a04407980abb75a6.jpg
        //https://+ 桶名+ . + 地域节点 + / + "avatar"+dateStr+fileName
        String path = "https://static.newdc.org.cn/"+fileName;
        System.out.println(path);
        return  path;
    }
 
    public static void main(String args[]){
    }
 
 
}