引入maven
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.5.7</version>
</dependency>
java代码如下
private static String secretId = "????";
private static String secretKey = "???";
private static String bucketName = "haotuo-1253655360";
private static String regionStr = "ap-shanghai";//区域北京则 beijing
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("C:\\Users\\Administrator\\Desktop\\202204161438 SS-101286 update@S 350g 白卡 光膜 3744-01.jpg");
list.add("C:\\Users\\Administrator\\Desktop\\202204160921 23095150@W 不干胶 各 1440-01.jpg");
update(list);
}
public static void update(List<String> list){
COSClient client = null;
if(client!=null) {
client.shutdown();
}
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
Region region = new Region(regionStr);
ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
client = new COSClient(cred, clientConfig);
try {
for(String path:list){
// 指定要上传的文件
File localFile = new File(path);
// 指定要上传到的存储桶
// 指定要上传到 COS 上对象键
String key = localFile.getName();
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
client.putObject(putObjectRequest);
}
} catch (CosServiceException serverException) {
serverException.printStackTrace();
} catch (CosClientException clientException) {
clientException.printStackTrace();
}
}