Understand at a glance: Ali cloud OSS

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sunnyzyq/article/details/101678657

[Introduction]

In today's Internet industry, Huawei, Baidu, Tencent, Alibaba and other large companies chasing gradually, increasing scale.

These company chiefs knew all the disadvantages of the Internet industry, the gradual extension of a lot of service, dedicated to the medium and small companies to provide convenient.

For example, your company to develop a system to use face recognition technology. The company generally is not to develop a set of their own, that time-consuming and laborious.

But these services, these companies would have to develop a good big brother, you just need to pay some costs, they call related interfaces can be achieved.

There are a lot of services like this, they are together, they form a cloud market. Huawei cloud, cloud Ali, Baidu and other cloud resulting.

[Special Statement]

 Non-advertising this blog blog, Ali Baba did not need me to do promotion, I have not the component.

 Only recently docking Ali cloud OSS, through the middle of a little ups and downs, to pave the way for the majority of people later, hereby summarized.

[01] What is Ali cloud?

This is Ali cloud official website: https://www.aliyun.com

(1.1) after opening the official website, it is like this.

(1.2) As you can see, there are many ways to classify the left side, below each category there are a number of services. Like a shopping site, on the left lists a series of sell things, but these things make more use of the Internet-related service-based, this is Ali cloud.

[2] What is Ali cloud OSS?

(2.1) First, we click on the object storage service [OSS]

(2.2) The following is a brief introduction Ali cloud red box OSS, in short, he is a file storage service.

For example, your website development, assumptions need to use image upload, file upload functions.

Before we approach it is to build your own image server or a file server, and then write their own set of related implementation.

But now, if we use the words of Ali cloud OSS, which together will save us money to buy servers and write time code implementation.

【3】注册与登录

既然要使用人家的功能,总不能白白使用,肯定是需要一个账号付费的,左边登录,右边注册,自己看着办。

有账号的点登录,没账号的先注册一个,然后登录。已经登录的就当我没说。

【4】开通OSS

(4.1)登录成功后,如果没有开通OSS的,则点击【立即开通】,然后他会提示相关方案。

开通的过程中他会让你选择个人版还是企业版,后面的API接口都一样,自己根据业务需要开通过即可。

(4.2)如果已经开通,则可以看到【管理控制台】,我们点击它。

【5】创建 Bucket

(5.1) 来到下面这个页面后,我们点击【创建Bucket】。

Bucket 的中文意思是桶,桶是用来干嘛的?当然是装东西的。也就是官方说的存储空间。

你可以理解为你电脑中的C盘、D盘、E盘等。

(5.2)创建Bucket的时候,会让你填写相关信息,一般来说我们只填名称和选择区域,其他都默认就好。

Bucket名称是唯一的,这个唯一同其他所有用户,比如sunny这个桶已经有人用了,那么你就不能用这个了。至于是谁用的,你不知道,我也不知道。

区域就是你想把你东西存放到哪个地方,从功能来说,选哪个都一样。由于我经常呆成都,所以就选了个最近的西南成都。

选择完毕后,点击【确定】完成创建。

【6】记住桶名(bucketName)和 外网地域节点(endPoint)

(6.1)完成创建后,就可以看到这个存储空间 sunnyzyq 了,右边是空间的相关介绍。记住这个桶名 (bucketName),等会撸代码的时候需要。

(6.2)另外,还有记住你的这个Endpoint,等会撸代码的时候也需要。

【7】创建AccessKey,获取授权信息

阿里云的OSS只是一个服务,我们要使用服务,需要 AccessId 和 AccessKey 进行授权。不光是阿里接口,几乎所有API接口都有这一步。

(7.1)点击如图位置【Access Key】,开始获取授权信息。

(7.2)第一次创建,会弹出这个东西。一般来说,个人的数据没有那么必要折腾,选择【继续使用AccessKey】。而如果是公司用的话,基于安全原因,一般选择【开始使用字用户AccessKey】

(7.3)选择完毕后,点击【创建AccessKey】。

(7.4)然后他会向你发验证码,确认是否是本人操作,输入验证码后,就会得到相关的的 AccessKeyId 和 AccessKeySecret 了。 

【8】确保余额充足

现在万事俱备,只差充值。阿里不可能让你白玩的,多冲少冲,多少要冲,我们往账户冲点钱,多少由你自己决定。

【9】阿里云OSS实战

空间现在有了,金额有了,授权有了,我们就可以实际操作一把了。

OSS具体开发文档链接地址为:https://help.aliyun.com/product/31815.html

例如:我们往上面上传一张图片。

(9.1)打开我们的Eclipse。

(9.2)然后新建一个oss的maven工程。

 (9.3)在pom.xm中引入oss关联的依赖

<dependency>
	<groupId>com.aliyun.oss</groupId>
	<artifactId>aliyun-sdk-oss</artifactId>
	<version>3.5.0</version>
</dependency>

(9.4)创建包 com.aliyu.oss,包下创建类 AliyunOssUntil.java

(9.5)我们去百度找一张漂亮小姐姐的图片,保存到桌面。

(9.6)然后把她拷贝到项目中,放在最外层根目录里。

(9.7)然后我们就开始进入最关键的撸代码部分了。

我们给上传的图片重新取个名字,叫为: zhaoliying.jpg 。

package com.aliyun.oss;

import java.io.File;

public class AliyunOssUntil {
	
	public static final String endpoint = "你的endpoint";
	public static final String accessKeyId = "你的accessKeyId";
	public static final String accessKeySecret = "你的accessKeySecret";
	public static final String bucketName = "你的桶名";

	public static OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
	
	public static void main(String[] args) {
		ossClient.putObject(bucketName, "zhaoliying.jpg", new File("zly.png"));
		ossClient.shutdown();
		System.out.println("上传图片完成!");
	}

}

(9.8)OK,代码撸好了,我们执行下。控制台显示了,说明程序已经执行完毕。

(9.9)打开我们的华为云OSS管理面板,看看是否真的上传上去了呢?点击【文件管理】。

(9.10)可以看到图片是真的上传上来了,并且显示为了我们取名后的名称。

(9.11)点击【详情】,可以查看具体内容。

(9.12)这样我们就可以看到图片内容了。

(9.13)我们把这个链接直接用浏览器访问,还可以触发下载文件。

(9.14)如果是图片的话,引用 <img> 标签显示,可以直接显示。

(9.15)打开效果如下图。

总结:OK,阿里云的 OSS 如何使用已经介绍完成了,虽然只举了一个例子,但整个流程已经打通完毕。

其他的接口请自行参考官方API文档:阿里云 OSS 官方 API 文档

我码了这么多字,你看了这么久,大家都不容易!我祝你万事如意,你给我点个赞呗!

 

Guess you like

Origin blog.csdn.net/sunnyzyq/article/details/101678657