Java implements Alibaba Cloud file storage OSS

Table of contents

 Why use cloud storage

SpringCloud Alibaba-OSS

Buy Alibaba Cloud Storage

Use Alibaba Cloud package for storage

import dependencies

test code

springcloud alibaba package


 Why use cloud storage

 Direct link: Object Storage OSS_Cloud Storage Service_Enterprise Data Management_Storage-Alibaba Cloud

SpringCloud Alibaba-OSS

Object Storage Service (OSS) is a massive, secure, low-cost, and highly reliable cloud storage

service, suitable for storing any type of file. Elastic expansion of capacity and processing power, multiple storage types to choose from, fully optimized

minimize storage costs.

Buy Alibaba Cloud Storage

Let's click to buy directly.

 As you can see, the price has exceeded expectations, so I choose! Do not buy! ! ! !

Sudden changes will make my poor family even worse! ! ! ! ! ! !

(Later I learned about Qiniuyun through Baidu: data storage, if your data storage does not exceed 10GB, then you are free. If your storage is between 10GB and 50GB, you will be charged 0.15 per GB RMB. If your storage exceeds 50GB, then you will be charged RMB.14 per GBO. These fees are calculated and charged monthly.)

 Later learned:

Flip down, you can see

 

 

 Since I can prostitute others for nothing, I don't buy it and just upload the code.



Use Alibaba Cloud package for storage

In fact, after you purchase, you can find the tutorials provided by Alibaba Cloud on the purchase page. You only need to follow the tutorials to complete cloud storage locally using java.

import dependencies

        <!-- 阿里云对象存储
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>-->

test code

@Test
	public void testUpload() throws FileNotFoundException{
		// Endpoint以杭州为例,其它Region请按实际情况填写。
		String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
		// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
		String accessKeyId = "<yourAccessKeyId>";
		String accessKeySecret = "<yourAccessKeySecret>";

		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

		// 上传文件流。
		InputStream inputStream = new FileInputStream("文件路径");
		ossClient.putObject("你的存储桶名称", "对象的名字(存储文件的名字)", inputStream);

		// 关闭OSSClient。
		ossClient.shutdown();
	}

After running, go to the cloud storage to view, and you can see the uploaded files.

springcloud alibaba package

Direct link: https://github.com/alibaba/spring-cloud-alibaba/blob/2022.x/README-zh.md

Direct link (Aliyun): https://github.com/alibaba/aliyun-spring-boot/tree/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample

spribg-cloud-alibaba packaging steps:

1. Introduce oss-starter

2. Configure key and endpoint related information

3. Use OSSClient to perform related operations

Guess you like

Origin blog.csdn.net/Hubery_sky/article/details/131758694