OSS Cloud Storage Management Practice Teaching

OSS Cloud Storage Management Practice

Scene experience goal

After starting the experiment, the system will automatically create a Linux ECS. Among them, web applications and OSS python SDK have been deployed in the created ECS. In this experiment, the created ECS is used as a web server. Directly accessing its IP address in the browser will display a page containing four small pictures, and the four pictures are saved locally in the ECS. Upload the four pictures to the OSS by calling the OSS API, and delete the four pictures saved locally in the ECS. At this time, when you visit the ECS IP, you will see that there are no pictures to display on the page. In order to display the original four pictures on the page, the experiment needs to modify the picture address in the default access page under the root directory of the web application server, and modify the picture address to the access address of the four pictures in the OSS.

img

After experiencing this scenario, you can master the following abilities:

  1. Manage the object storage service OSS through the Alibaba Cloud management console

  2. Use the object storage service OSS to store the pictures in the page in actual web development

  3. Call OSS API to upload pictures

background knowledge

Alibaba Cloud Object Storage Service (OSS) is a massive, secure, low-cost, and highly reliable cloud storage service provided by Alibaba Cloud. Users can upload and download data in any application, any time, and any place by calling the API, or simply manage data through the user's web console. OSS is suitable for storing any file type, suitable for various websites, development companies and developers.

Alibaba Cloud OSS Image Service (IMG) is a massive, secure, low-cost, and highly reliable image processing service provided by Alibaba Cloud OSS. The user uploads the original picture and saves it on OSS, and processes the picture at any time, any place, and any Internet device through a simple RESTful interface. The image processing service provides an image processing interface. For image upload, please use the OSS upload interface. Based on IMG, users can build services related to pictures. The images processed by the image service come from OSS, so the upload of the image is actually uploaded to the bucket of the same name in OSS.

In real work scenarios, pictures, audios, videos, etc. are generally stored in the OSS service, which can greatly improve the user's page access speed.

Image website based on single ECS

  1. This section will show a picture sharing website built on ECS and the source code of the picture sharing website.

  2. First, create a new page in the browser. Then, in the address field of the browser, the input of this experiment ECS allocated instance elasticity the IP . Check out that the current page shows a picture of "Garden", which is actually composed of 4 small pictures.

img

  1. Log in to the ECS server remotely, view pictures and share the source code of the website. Please login to use this test cloud products and resources provided by ECS server elastic IP , user name and password .

Note: This experiment ECS server for the Linux operating system, detailed steps ECS remote login instance, please refer to cloud the help documentation to connect remotely Linux system.

img

  1. After logging in to the ECS server, execute the following command to enter the root directory of the website system.
cd /alidata/www/default

Then, use the following view command to view the four pictures 01.png, 02.png, 03.png and 04.png saved in the root directory, which are the 4 small pictures displayed on the picture sharing website.

ls

img

In the browser, create a new page and enter <ECS instance external network address>/01.png in the address bar to view the picture content of 01.png. In the same way, you can view the image content of “02.png”, “03.png” and “04.png”.

img

  1. Use the following command to view the source code of the homepage of the website, and you can see the pictures displayed on the picture sharing page. Read pictures 01.png, 02.png, 03.png and 04 from the website root directory /alidata/www/default .png Stitched.
cat index.html

img

Use ECS+OSS experimental picture website

  1. This section uploads the pictures stored locally to the OSS Bucket , so that the picture sharing website will read the pictures from the OSS Bucket , and there is no need to save the pictures locally.

  2. In the /alidata/www/default directory of the ECS instance , execute the following command to enter the edit page of cfg.json and modify the configuration information of OSS.

vim cfg.json

img

Enter " I " on the keyboard to switch to the edit mode. In the edit page, modify the following parameter information.

  • id: AK ID value allocated by cloud product resources ;
  • secret: AK Secret value allocated by cloud product resources ;
  • endpoint: Please OSS area according to the following correspondence table and the Endpoint, input resources allocated cloud product area corresponding to the external network Endpoint . For example: if the assigned area is East China 1 (Hangzhou), enter oss-cn-hangzhou.aliyuncs.com;
  • Bucket : OSS resource bucket name for cloud product resource allocation ;
  • object_dir: OSS resource object path for cloud product resource allocation .

After finishing, first press the esc key to exit the editing state, and then enter the following command to save and exit the editing page.

 :wq

Table of correspondence between OSS regions and external network endpoints:

area Extranet Endpoint
East China 1 (Hangzhou) oss-cn-hangzhou.aliyuncs.com
East China 2 (Shanghai) oss-cn-shanghai.aliyuncs.com
China North 1 (Qingdao) oss-cn-qingdao.aliyuncs.com
China North 2 (Beijing) oss-cn-beijing.aliyuncs.com
South China 1 (Shenzhen) oss-cn-shenzhen.aliyuncs.com

img
3. Execute the following command to upload 01.png to OSS. The script uses the API interface put_object_from_file() provided by OSS to upload local files to the Object path of cloud product resource allocation .

python oss_upload.py 01.png

Then, execute the same command, only the 01.png changed 02.png , etc., will 02.png , 03.png and 04.png three pictures were uploaded to the OSS. Next, the user will access the OSS management console and view the files uploaded to OSS.

Note: For the specific method of uploading local files to OSS via Python SDK, please refer to experiment SL003 to upload and download files using OSS API .

img

  1. First, click on the left side of the page Cloud product resources , and then click a button **** replication login url , login to the console with a browser incognito window;

Ali cloud RAM login screen, enter ' cloud products and resources provided by the sub-user name , sub-account password , then click Login to enter Ali cloud management console.

img

img

  1. After a successful login, click on the left side of the list of products and services, then locate and click on the Home screen object storage OSS , OSS to enter the management console.

img

  1. After entering the OSS management console, in the bucket list, click the bucket name provided by the cloud product resource , such as adc-oss-969hd2, to enter the adc-oss-969hd2 Bucket management page.

img

  1. Click [File Management], you can view that there are one or more folders in the current Object list. Click the first-level Object path of cloud product resource allocation . For example, if the object path assigned by the user is OSSCTY/u-xxx/, OSSCTY is the first-level object path. Click OSSCTY to enter the Object "OSSCTY" path.

img

In the Object path of "OSSCTY", click the assigned secondary Object path . For example, if the object path assigned by the user is OSSCTY/u-xxx/, u-xxx is the secondary object path. Click u-xxx to enter the Object OSSCTY/u-xxx path.

At this time, in the first row of the list managed by the object, the user can view the path information of the current object. In the Object list managed by the Object, 4 pictures that have been uploaded and saved to the "SOSSCTY/u-xxx /" path are displayed: 01.png, 02.png, 03.png, 04.png.

img

  1. Return to the ECS instance terminal page and execute the following command to enter the alidata/www/default directory.
cd /alidata/www/default

Then execute the following command to delete all " .png " files in the current directory , that is, the 4 pictures 01.png, 02.png, 03.png and 04.png read from the picture sharing website.

rm -rf *.png

Execute the view file command to check that there is no picture file in the current directory, which proves that the picture has been deleted.

ls

img

  1. Open (in the address bar of the browser, enter the ECS external network address allocated by the cloud product resource and visit) or refresh the main page of the picture sharing website, and check that all pictures on the current page cannot be displayed normally. This is because the picture sharing website reads local picture files, and the locally stored picture files have been deleted, so the picture information cannot be read.

Note: If images can still be displayed on the accessed page, it may be due to a browser cache problem. It is recommended to clear the browser cache or change a browser to view the ECS server homepage.

img

  1. Next, you will reconfigure the image access information in index.html , and follow the steps below to modify the image link to the URL access address of the image uploaded to OSS:
  1. Return to the OSS management console, enter the Object path of the cloud product resource allocation , and obtain the URL access address of the picture. Click 01.png , find and click [Copy File URL] in the pop-up box on the right.

img

  1. Execute the following command to return to the /alidata/www/default directory of the ECS server, and enter the editing interface of the index.html on the server access homepage.
cd /alidata/www/default

vim index.html
  1. Under ** **, find the link parameter src=“01.png” of the first picture , delete 01.png, and paste the copy in the above step and upload it to OSS 01.png to access the picture

address. Similarly, modify the link addresses of 02.png, 03.png, and 04.png to the storage addresses of OSS.

img

  1. After finishing all the picture modification, click the esc key to exit the editing state, execute the following command to save and exit the editing page.
:wq
  1. In the browser, refresh ECS examples of external network addresses to access the page, the page will show pictures of the garden again, indicating that the site can now use images stored on the OSS to display the page.

img

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/113702817