oss 上传测试
This commit is contained in:
parent
9466c1f262
commit
0e86dbff14
@ -0,0 +1,5 @@
|
|||||||
|
package org.example.shop.service;
|
||||||
|
|
||||||
|
public interface OSSService {
|
||||||
|
boolean upload();
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -38,4 +38,7 @@ spring:
|
|||||||
ssl:
|
ssl:
|
||||||
enable: true
|
enable: true
|
||||||
|
|
||||||
|
oss:
|
||||||
|
endpoint: oss-cn-chengdu.aliyuncs.com
|
||||||
|
key-id: LTAI5t7EopaqeKmKmQv3Nx9e
|
||||||
|
key-sec: 4xxNw7RgEBK4quCwaPapItQ6WNvBcG
|
||||||
|
33
user-service/src/test/java/org/example/shop/TestOss.java
Normal file
33
user-service/src/test/java/org/example/shop/TestOss.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user