background

With the development of the Internet today, almost all cloud vendors provide “object storage service”. A massive, secure, low-cost, highly reliable cloud storage service, suitable for storing files of any type. Flexible expansion of capacity and processing capability, multiple storage types for selection, comprehensive optimization of storage costs.

When we use the products of the corresponding cloud vendor, we only need to introduce the SDK provided by the corresponding attempt and implement it according to its development documents. However, when we have access to a large number of cloud vendors (or are able to ensure horizontal interface migration), we need to “destructively modify” the target vendor interface.

Examples of SDK upload from several vendors are provided as follows:

Ali cloud

// Endpoint This section uses Hangzhou as an example. Enter other regions based on actual conditions.

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 PutObjectRequest.

String content = "Hello OSS";

PutObjectRequest putObjectRequest = new PutObjectRequest("<yourBucketName>"."<yourObjectName>".new ByteArrayInputStream(content.getBytes()));



// Upload a string.

ossClient.putObject(putObjectRequest);



// Close OSSClient.

ossClient.shutdown();

Copy the code

Huawei cloud

String endPoint = "https://your-endpoint";

String ak = "*** Provide your Access Key ***";

String sk = "*** Provide your Secret Key ***";

// Create ObsClient instance

ObsClient obsClient = new ObsClient(ak, sk, endPoint);



obsClient.putObject("bucketname"."objectname".new File("localfile")); // localfile indicates the path of the localfile to be uploaded. The file name must be specified

Copy the code

Seven NiuYun

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);

Copy the code

The solution

Amazon S3 agreement

Amazon is the earliest provider of object storage service and has formulated industry standards related to file storage, which means that only S3 protocol can be implemented to access file storage vendors and middleware compatible with this protocol. Of course, S3 protocol is not only technical implementation requirements standards, for availability and other specific requirements.

Compatible with S3 domestic cloud vendors

The name of the address
Ali cloud https://www.aliyun.com
Huawei cloud https://www.huaweicloud.com
Tencent cloud https://cloud.tencent.com
Seven NiuYun https://www.qiniu.com
Kingsoft cloud group https://www.ksyun.com

How to use

  • Introduce dependencies. Introducing this dependency eliminates the need to introduce cloud vendor SDKS
<dependency>

    <groupId>com.pig4cloud.plugin</groupId>

    <artifactId>oss-spring-boot-starter</artifactId>

    <version>hundreds</version>

</dependency>

Copy the code
  • Configuration file Storage
oss:

  path-style-access: false    {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     The bucket name created above

Copy the code
  • operation


@Autowire

private final OssTemplate ossTemplate;



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

Copy the code

Supports 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

Copy the code
  • Configure miniO parameters
# file system

oss:

  path-style-access: true

  endpoint: http://IP:9000

  access-key: lengleng

  secret-key: lengleng

  bucketName: lengleng

Copy the code
  • Upload using OssTemplate

Source code address:

Github.com/pig-mesh/os… Welcome fork extension