Current location - Quotes Website - Signature design - Xiaobai uses Alibaba Cloud's oss to implement file cloud storage
Xiaobai uses Alibaba Cloud's oss to implement file cloud storage

The project needs to upload avatars and does not want to store them locally. I have used other Alibaba Cloud products before, so I will use Alibaba Cloud's oss here. I have to say that the steps for using Alibaba Cloud's products are really clear. Here are the instructions Let’s take a look at my own usage steps.

Select object storage oss and activate it

There is no fee to activate, and there is a certain free quota

Permissions can be set to public** *Reading

Actually it is two steps

1. Import pom

2. Copy and paste method

Due to some secrets of Alibaba Cloud Key configuration, regional nodes, buckets, etc. are constant-level, so I extracted them here and put them in application.properties for easy management. I manually encrypted the data.... You can just replace it with your own,

These configuration key=value keys are written casually by myself (not at least, at least the name means it), just so that a configuration class we configure can use spring's dependency injection to fill in the value

< p> Description:

Description:

The URL of our Alibaba Cloud oss ??warehouse is our warehouse + fixed regional node value + our file name, so here in order to upload The file names are not repeated, and an idworker is used. If you don’t know, you can take a look at Twitter’s Snowflake (snowflake algorithm)

After you have built it, you can use postman to test it

ps: If we To facilitate management or later expansion, we can also introduce a tool here

We can use a method of this tool, String dirpath=new DateTime().tostring("yyyy/MM/dd"); to convert the current Convert the time to the format of yyyy/MM/dd, such as 2020/02/03

In this way, when we upload the file name, we can use this dirpath as the name of the folder where our pictures are located, and use the distributed id The id generated by the generator is stored as a name.

Let’s take a look at the effect