Alibaba Cloud Object Storage OSS Service-Upload/Delete/Get Pictures
1. Preparation
- Open object storage OSS service
- Create AccessKey (required when using Alibaba Cloud OSS SDK)
- Create a bucket (input a unique bucket name, select the appropriate area, other options default)
- Create Spring Boot project
- It is recommended to use Postman to test the back-end API. For the download and use of Postman, please refer to https://blog.csdn.net/fxbin123/article/details/80428216
- Note: When using Postman to test the upload image function, you need to set the type of the request body (Body) to form-data
2. Upload pictures
// 未对异常进行处理
@PostMapping(value = "/image")
public void uploadImage(MultipartFile file) {
// MultipartFile代表HTML中form data方式上传的文件
// EndPoint以上海为例
String endPoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云API秘钥
String accessKeyId = "<your access key id>";
String accessKeySecret = "<your access key secret>";
String bucketName = "<bucket name>";
OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
client.putObject(bucketName, imageName, new ByteArrayInputStream(file.getBytes()));
client.shutdown();
}
3. Delete picture
// 未对异常进行处理
@DeleteMapping(value = "/image/{imageName}")
public void deleteImage(@PathVariable String imageName) {
// EndPoint以上海为例
String endPoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云API秘钥
String accessKeyId = "<your access key id>";
String accessKeySecret = "<your access key secret>";
String bucketName = "<bucket name>";
OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
client.deleteObject(bucketName, imageName);
client.shutdown();
}
4. Get pictures
// 未对所有异常进行处理
@GetMapping(value = "/image/{imageName}")
public byte[] getImage(@PathVariable String imageName) {
// EndPoint以上海为例
String endPoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云API秘钥
String accessKeyId = "<your access key id>";
String accessKeySecret = "<your access key secret>";
String bucketName = "<bucket name>";
OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
OSSObject image = client.getObject(bucketName, imageName);
InputStream content = image.getObjectContent();
if (content != null) {
try {
// 设置一个足够大的buffer用于存储图片的比特数据
int length = 1920 * 1260 * 3;
byte[] buf = new byte[length];
int size = 0;
int temp;
while ((temp = content.read()) != -1) {
buf[size] = (byte) temp;
size++;
}
content.close();
// 对缓冲区进行裁剪后,将图片以字节数组的形式返回
return Arrays.copyOf(buf, size);
} catch (IOException e) {
System.out.println("exception");
return null;
}
}
client.shutdown();
return null;
}
5. Test the function of getting pictures
Copy and paste the content to a file in the src/main/resources/static directory of the Spring Boot project
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Get Image</title>
</head>
<body style="margin: 0;">
<!-- 测试获取OSS图片功能是否正确 -->
<img src="http://localhost:80/image/<imageName>"/>
</body>
</html>
Alibaba Cloud Object Storage OSS official document: https://help.aliyun.com/product/31815.html?spm=5176.8466032.0.0.7e8714505JnJY6