当前位置:网站首页>OSS cloud storage management practice (polite experience)

OSS cloud storage management practice (polite experience)

2022-04-23 13:43:00 Alibaba cloud cloud Lab

Alibaba cloud object storage
Alibaba cloud object storage (Object Storage Service, abbreviation OSS), It's Alibaba cloud's massive supply to the outside world 、 Security 、 Low cost 、 Highly reliable cloud storage services . The user can call API, In any application 、 Anytime 、 Upload and download data anywhere , It can also be done through the user Web The console simply manages the data .OSS Suitable for storing any file type , Suitable for all kinds of websites 、 Development enterprises and developers use .
Alibaba cloud OSS Image processing services
Alibaba cloud OSS Image processing services (Image Service, abbreviation IMG) , It's alicloud OSS The vast amount of foreign supplies 、 Security 、 Low cost 、 High reliable image processing service . The user uploads and saves the original image in OSS On , Through simple RESTful Interface , At any time 、 Any place 、 Processing images on any Internet Device . Image processing service provides image processing interface , Please use OSS Upload interface . be based on IMG, Users can build services related to pictures . The image service processes images from OSS, So the upload of pictures is actually to OSS The same name of Bucket in .

In real work situations , In general, pictures 、 Audio 、 Videos, etc. are stored in OSS In service , This can greatly improve the user's page access speed .11 month 9 solstice 11 month 23 During the day , Available upon deployment “TOMY A domeca alloy car model ”.

O1CN019If3vm1vDFjunR9QE_!!2921416138-0-cib.jpg

Address :https://developer.aliyun.com/adc/series/activity/1111

Experience preparation

You need to use your own account to open the above services
ECS Resources to address
After starting the experiment , The system will automatically create a Linux ECS. among , Created ECS Has been deployed in web The application and OSS python SDK. In this experiment , Created ECS As web The server , Access it directly from the browser IP The address will display a page with four small pictures , And all four pictures are saved in ECS Local . By calling OSS API Upload four pictures to OSS in , Also delete save in ECS Four local pictures , Visit at this time ECS IP You'll see that there are no pictures to show on the page . In order to display the original four pictures in the page , It needs to be modified in the experiment web The image address in the default access page under the root directory of the application server , Change the picture address to four pictures in OSS Access address in .

Connect ECS The server

1、 Double click to open the virtual desktop Chromium browser , stay RAM Click next... In the user login box , Copy the sub user password of the cloud product resource list , Press CTRL+V Paste the password into the password input area , Login sub account ( All subsequent pasting operations on the remote desktop use CTRL + V Shortcut key ).
2、 Copy ECs ECS Console address , stay Chromium The browser opens a new tab , Paste and access ECs ECS Console .
3、 In the navigation bar on the left side of the console , Click on the instance .
p349123.png
4、 In the menu bar at the top of the instance page , Switch to the region where the resource is located .

explain :

Refer to the following picture to switch the region to East China 2( Shanghai ).
p349125.png
5、 On the instance page , Click Remote Connection... Under the action column of the target instance .

explain :

You can view the target instance in the cloud product resource list ID.
p349128.png

6、 In the remote connection and command dialog box Workbench In the remote connection area , Click log in now .

p349130.png
7、 In the login instance dialog box , Enter in the cloud resource product list ECS Login password of the server , Click OK .

p349131.png
Return to the following page , Indicates that you have successfully connected to ECS The server .
e12696abe6c6446e8cc9d9f80ba61c52.png

Based on single ECS The picture website of

This section will show an example in ECS The picture sharing website that has been set up on , And the source code of the image sharing website .

1. stay Chromium The browser opens a new tab , Enter in the address field http://<ECS Public address () elastic IP)>:8080 And access . You can view the display in the current page 1 Zhang “ The garden ” Pictures of the , The image is actually created by 4 It's a little picture of .
9eba88af045e4fa28fff26d4ac22a6c3.png

2. Switch to the terminal page . Execute the following command , Enter the root directory of the website system .

cd /alidata/www/default

then , Use the following view command , Look at the four pictures saved in the root directory 01.png,02.png,03.png and 04.png, That's what the picture sharing website shows 4 A little picture .

ls

TB1JTsAtxD1gK0jSZFyXXciOVXa-670-76.png

stay Chromium The browser opens a new tab , Enter in the address bar <ECS Instance Internet address >/01.png, You can see that 01.png The picture content of . Empathy , You can see “02.png”, “03.png” as well as “04.png” The picture content of .
TB18rY0ueH2gK0jSZJnXXaT1FXa-507-292.png
3. Use the following command , Check the source code of the home page of the website , You can see the pictures displayed in the picture sharing page , It's from the site root /alidata/www/default in , Read the picture 01.png,02.png,03.png and 04.png Spliced .

cat index.html

TB1O_gutpY7gK0jSZKzXXaikpXa-699-562.png

Use ECS+OSS Realize picture website

1. This section uploads images stored locally to OSS Bucket in , thus , The image sharing website will start from OSS Bucket Read pictures from , There is no need to save pictures locally .

2. stay ECS Example of /alidata/www/default Directory , Execute the following command , Get into cfg.json Edit page for , And modify it OSS Configuration information .

vim cfg.json

TB1aUgwtrr1gK0jSZFDXXb9yVXa-579-37.png

Type... On the keyboard “I”, Switch to edit mode , In the edit page , Modify the following parameter information .

id: Cloud product resources The distribution of AK ID value ;
secret: Cloud product resources The distribution of AK Secret value ;
endpoint: Please follow the following OSS Region and Endpoint Correspondence table , Input Cloud product resources Allocated regional Corresponding Extranet Endpoint . for example : The distribution of regional by East China 1( Hangzhou ), Then input. oss-cn-hangzhou.aliyuncs.com;
bucket: Cloud product resources The distribution of OSS resources Bucket name ;
object_dir: Cloud product resources The distribution of OSS resources Object route .
After completion , First click esc key Exit edit status , Then enter the following command to save and exit the edit page .

 :wq

OSS regional And Extranet Endpoint Correspondence table :

regional

Extranet Endpoint

East China 1 ( Hangzhou )

oss-cn-hangzhou.aliyuncs.com

East China 2 ( Shanghai )

oss-cn-shanghai.aliyuncs.com

The north China 1 ( Qingdao )

oss-cn-qingdao.aliyuncs.com

The north China 2 ( Beijing )

oss-cn-beijing.aliyuncs.com

south China 1 ( Shenzhen )

oss-cn-shenzhen.aliyuncs.com

TB1xfLVukL0gK0jSZFAXXcA9pXa-1272-140.png

3. Execute the following command , take 01.png Upload to OSS, The script uses OSS Provided API Interface put_object_from_file() Upload local files to Cloud product resources The distribution of Object route in .

python oss_upload.py 01.png
then , Execute the same command , Only 01.png Change it to 02.png etc. , take 02.png、03.png and 04.png Three pictures are uploaded to OSS. Next , Users will visit OSS Administrative console , And view the upload to OSS The file of .

explain : adopt Python SDK Upload local files to OSS The specific method of , Please refer to the experiment SL003 Use OSS API Upload and download files .

TB1pWMBtAT2gK0jSZFkXXcIQFXa-531-190.png

4. Copy OSS Console address , stay Chromium The browser opens a new tab , Paste and access OSS Console .

https://oss.console.aliyun.com/
5. Get into OSS After managing the console , stay Bucket In the list , Click on Cloud product resources Provided Bucket name , for example adc-oss-969hd2, Get into adc-oss-969hd2 Bucket Manage Pages .

TB1Soq.v8r0gK0jSZFnXXbRRXXa-1536-833.png

6. Click on 【 file management 】 , You can see the current Object One or more folders exist in the list . Click on Cloud product resources The level of distribution Object route . for example : User assigned Object route by OSSCTY/u-xxx/, be OSSCTY It's level one Object route . Click on OSSCTY, Get into Object “OSSCTY” In the path .

TB1IPS9v1H2gK0jSZJnXXaT1FXa-1382-571.png

stay “OSSCTY” Of Object In the path , Click on the assigned secondary Object route . for example : User assigned Object route by OSSCTY/u-xxx/, be u-xxx It's level two Object route . Click on u-xxx Get into Object OSSCTY/u-xxx In the path .

here , stay Object In the first line of the managed list , Users can view the current Object Path information . stay Object Managed Object In the list , The presentation has been uploaded and saved to “SOSSCTY/u-xxx /” The next path 4 A picture :01.png,02.png,03.png,04.png.

TB1PzS9v1H2gK0jSZJnXXaT1FXa-1212-703.png

7. Switch back to the terminal page , Execute the following command to enter alidata/www/default Catalog .

cd /alidata/www/default
Then execute the following command , Delete all... In the current directory “ .png ” file , That is to say , From the picture sharing website 4 A picture 01.png, 02.png, 03.png and 04.png.

rm -rf *.png

Execute the view file command , View no picture files in current directory , Prove that the picture has been deleted .

ls

TB1N5kBtrj1gK0jSZFuXXcrHpXa-578-93.png

8. open ( In the browser's address bar , Input Cloud product resources The distribution of ECS Internet address And access ) Or refresh the main page of the image sharing website , All the pictures in the current page cannot be displayed normally . This is because the image sharing website reads local image files , The image file stored locally has been deleted , So we can't read the picture information .

Be careful :

If you visit the page, you can still display pictures , Maybe it's due to browser caching , It is suggested to clean up the cache of the browser or view it in a different browser ECS Server home page .

TB1VyzSuXT7gK0jSZFpXXaTkpXa-814-603.png

9. Next , Will be reconfigured index.html Picture access information in , Follow these steps to change the image link to upload to OSS The picture of URL Access address :

  1. Switch back to the OSS Management console page , Get into Cloud product resources The distribution of Object route in , Get the URL Access address . Click on 01.png , Find and click... In the pop-up box on the right 【 Copy file URL】.

TB1vtK9vW61gK0jSZFlXXXDKFXa-1573-740.png

  1. Switch back to the terminal page , Execute the following command , return ECS Server's /alidata/www/default Under the table of contents , And go to the server to visit the home page index.html The editing interface of .
cd /alidata/www/default
vim index.html
  1. stay Next , Find the link parameters for the first picture src=“01.png”, Delete 01.png, And paste the copy above to OSS 01.png Access to pictures

Address . Empathy , modify 02.png,03.png,04.png The link address of is OSS Storage address .

TB1V_2Uumf2gK0jSZFPXXXsopXa-965-537.png

  1. After all the pictures have been modified , Click on esc key Exit edit status , Execute the following command to save and exit the edit page .
:wq

10. In the browser , Refresh ECS Example of Internet address The visit page of , The page will be displayed again The garden Pictures of the , Now the website can be stored in OSS To display the page .

TB1NsjUuoT1gK0jSZFrXXcNCXXa-537-605.png

版权声明
本文为[Alibaba cloud cloud Lab]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230601372433.html