"One stop" is compatible with the Spring Boot implementation of file storage for all cloud vendors

background

Today, with the development of the Internet, almost all cloud manufacturers provide object storage services. A massive, safe, low-cost and highly reliable cloud storage service, which is suitable for storing any type of files. Flexible expansion of capacity and processing capacity, multiple storage types to choose from, and comprehensively optimize the storage cost.

When we use the products of the corresponding cloud manufacturer, we only need to introduce the SDK provided by the corresponding attempt and implement it according to its development documents. However, when we access more cloud vendors (or can ensure the horizontal migration of the interface), we should make destructive modifications according to the interface of the target vendor.

The following provides examples of SDK uploading of several manufacturers' interfaces:

Alibaba cloud

// Endpoint takes Hangzhou as an example. Please fill in other regions according to the actual situation.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Create a PutObjectRequest object.
String content = "Hello OSS";
PutObjectRequest putObjectRequest = new PutObjectRequest("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()));

// Upload string.
ossClient.putObject(putObjectRequest);

// Close the OSSClient.
ossClient.shutdown();

Hua Weiyun

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an ObsClient instance
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

obsClient.putObject("bucketname", "objectname", new File("localfile")); // localfile is the path of the local file to be uploaded, which needs to be specified to the specific file name

Seven cattle cloud

Configuration cfg = new Configuration(Region.region0());
UploadManager uploadManager = new UploadManager(cfg);
String accessKey = "your access key";
String secretKey = "your secret key";
String localFilePath = "/home/qiniu/test.png";
String key = null;

Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(localFilePath, key, upToken);

Solution

Amazon S3 protocol

Amazon is the first manufacturer to provide object storage services and formulate industry standards related to file storage, which means that it only needs to implement S3 protocol to access file storage manufacturers and middleware compatible with this protocol. Of course, S3 protocol is not only a technical implementation standard, but also has specific requirements for usability.

Domestic cloud manufacturers compatible with S3 protocol

name address
Alibaba cloud https://www.aliyun.com
Hua Weiyun https://www.huaweicloud.com
Tencent cloud https://cloud.tencent.com
Seven cattle cloud https://www.qiniu.com
Jinshan cloud https://www.ksyun.com

How to use

  • Introduce dependencies. To introduce this dependency, you do not need to import the SDK of the cloud vendor
<dependency>
    <groupId>com.pig4cloud.plugin</groupId>
    <artifactId>oss-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</dependency>
  • Profile storage
oss:
  path-style-access: false    #Whether the request path is XXX/{bucketName}
  endpoint: s3-cn-east-1.qiniucs.com
  access-key: xxx    # key provided by cloud vendor
  secret-key: xxx    # Key provided by cloud vendor
  bucketName: pig4cloud     # Bucket name created above
  • operation
@Autowire
private final OssTemplate ossTemplate;

ossTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());

Support self built file storage such as MINIO

  • Create minio
docker run -p 9000:9000 --name minio1 \
  -e "MINIO_ACCESS_KEY=lengleng" \
  -e "MINIO_SECRET_KEY=lengleng" \
  minio/minio server /data
  • Configure minio parameters
# file system
oss:
  path-style-access: true
  endpoint: http://IP:9000
  access-key: lengleng
  secret-key: lengleng
  bucketName: lengleng
  • Use OssTemplate to upload

Source address:

https://github.com/pig-mesh/oss-spring-boot-starter Welcome to fork extension

Tags: Java

Posted by Stagnate on Wed, 25 May 2022 14:06:05 +0300