oss 上传测试

This commit is contained in:
LittleBoy 2021-10-25 17:17:33 +08:00
parent 9466c1f262
commit 0e86dbff14
4 changed files with 66 additions and 1 deletions

View File

@ -0,0 +1,5 @@
package org.example.shop.service;
public interface OSSService {
boolean upload();
}

View File

@ -0,0 +1,24 @@
package org.example.shop.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.example.shop.service.OSSService;
import org.springframework.beans.factory.annotation.Value;
public class OSSServiceImpl implements OSSService {
@Value("${oss.endpoint}")
private String endpoint;
@Value("${oss.key-id}")
private String accessKeyId;
@Value("${oss.key-sec}")
private String accessKeySecret;
private String bucket = "laowang";
@Override
public boolean upload() {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// ossClient.putObject("")
return false;
}
}

View File

@ -38,4 +38,7 @@ spring:
ssl: ssl:
enable: true enable: true
oss:
endpoint: oss-cn-chengdu.aliyuncs.com
key-id: LTAI5t7EopaqeKmKmQv3Nx9e
key-sec: 4xxNw7RgEBK4quCwaPapItQ6WNvBcG

View File

@ -0,0 +1,33 @@
package org.example.shop;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import java.io.File;
public class TestOss {
private String endpoint = "oss-cn-chengdu.aliyuncs.com";
private String accessKeyId = "LTAI5t7EopaqeKmKmQv3Nx9e";
private String accessKeySecret = "4xxNw7RgEBK4quCwaPapItQ6WNvBcG";
// 要上传的位置
private String bucket = "laowang-de-oss";
@Test
public void testUpload() {
File f = new File("D://a.jpeg");
if (!f.exists()) {
Assert.fail("文件不存在");
}
// 创建一个 oss客户端
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String saveFileName = "test/b.jpeg"; // 保存再oss中的文件名
// 上传文件
PutObjectResult res = client.putObject(bucket, saveFileName, f);
System.out.println(res);
}
}