阿里云对象存储oos入门教程--java
什么是对象存储oss
官网的描述:
链接: link
简单的理解就是对象存储是一种海量、安全、低成本、可靠的云存储服务,不需要占用本机硬盘的内存,也不用担心文件数据的丢失,并且享有自动化运维的服务。而在web开发中,前端涉及到文件上传的业务,前端请求文件上传请求服务端,在服务器本地磁盘当中,就不需要存储这个文件,直接上传到oss,由其进行存储和管理。
创建oss
-
登录阿里云 官网链接: link
-
登陆上以后点击右上角的控制台按钮;
-
将鼠标拖到左上角的菜单栏上;
-
找到对象存储oss并点击;
-
如果没有购买他会提示你没有开通需要购买(提示:如果你是学生的话可以去账号里进行实名认证,免费领取三个月试用的oss,如图)
点击立即查看
进入这样的界面往下滑
在这里可以领取三个月的oss
- 创建bucket
bucket(存储空间):用户存储对象的容器。
当你购买结束oss后,回到菜单中的对象存储oss之后可以看到这样的界面,此时点击bucket列表后创建bucket或者直接创建bucket;
创建bucket时,只需要取一个不重名的名字,再将读写权限改为公共读即可(如果错过了的话可以在bucket的设置中修改)
- 获取accesskey(密钥)
鼠标光标拖到右上角用户时会弹出这样的界面,点击
此时你需要创建你的accesskey
下面的步骤需要注意,经过验证后,accesskey secret在新版本为了安全性会只显示一次,需要你将其保存好再继续进行下一步,如果后续信息泄露后也可以在本界面禁用该accesskey 。
8.配置oss:下载sdd
在对象存储oss界面的菜单栏里最底部可以看到“sdk下载”,点击进入;
选择你使用的语言
这里我用的是java,所以我将以java为例进行后续操作,java1.7版本以上是可以不用下载的,可以通过maven进行配置直接点击其右上角的查看文档
点击左下角的在文档中心打开
在目录中找到开发参考/sdk参考/java/安装并点击
可以根据其描述找到自己对应的java版本并将代码粘贴到pom文件中
9.文件上传
同样在文档目录中找到如图所示的简单上传并点击
可以看到给了好几种对象的上传示例代码,找到你需要的相应格式进行使用就可以了,记得要将endpoint 、bucketname 、objectname 改写成自己需要的格式。credentialsprovider 是来设置自己的accesskey id和accesskey secrete。
上传字符串
以下代码用于将字符串上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
public static void main(string[] args) throws exception {
// endpoint以华东1(杭州)为例,其它region请按实际情况填写。
string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量oss_access_key_id和oss_access_key_secret。
environmentvariablecredentialsprovider credentialsprovider = credentialsproviderfactory.newenvironmentvariablecredentialsprovider();
// 填写bucket名称,例如examplebucket。
string bucketname = "examplebucket";
// 填写object完整路径,完整路径中不能包含bucket名称,例如exampledir/exampleobject.txt。
string objectname = "exampledir/exampleobject.txt";
// 创建ossclient实例。
oss ossclient = new ossclientbuilder().build(endpoint, credentialsprovider);
try {
// 填写字符串。
string content = "hello oss,你好世界";
// 创建putobjectrequest对象。
putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, new bytearrayinputstream(content.getbytes()));
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
// objectmetadata metadata = new objectmetadata();
// metadata.setheader(ossheaders.oss_storage_class, storageclass.standard.tostring());
// metadata.setobjectacl(cannedaccesscontrollist.private);
// putobjectrequest.setmetadata(metadata);
// 上传字符串。
putobjectresult result = ossclient.putobject(putobjectrequest);
} catch (ossexception oe) {
system.out.println("caught an ossexception, which means your request made it to oss, "
+ "but was rejected with an error response for some reason.");
system.out.println("error message:" + oe.geterrormessage());
system.out.println("error code:" + oe.geterrorcode());
system.out.println("request id:" + oe.getrequestid());
system.out.println("host id:" + oe.gethostid());
} catch (clientexception ce) {
system.out.println("caught an clientexception, which means the client encountered "
+ "a serious internal problem while trying to communicate with oss, "
+ "such as not being able to access the network.");
system.out.println("error message:" + ce.getmessage());
} finally {
if (ossclient != null) {
ossclient.shutdown();
}
}
}
上传byte数组
以下代码用于将byte数组上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
public static void main(string[] args) throws exception {
// endpoint以华东1(杭州)为例,其它region请按实际情况填写。
string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量oss_access_key_id和oss_access_key_secret。
environmentvariablecredentialsprovider credentialsprovider = credentialsproviderfactory.newenvironmentvariablecredentialsprovider();
// 填写bucket名称,例如examplebucket。
string bucketname = "examplebucket";
// 填写object完整路径,完整路径中不能包含bucket名称,例如exampledir/exampleobject.txt。
string objectname = "exampledir/exampleobject.txt";
// 创建ossclient实例。
oss ossclient = new ossclientbuilder().build(endpoint, credentialsprovider);
try {
// 填写byte数组。
byte[] content = "hello oss, 你好世界".getbytes();
// 创建putobjectrequest对象。
putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, new bytearrayinputstream(content));
// 创建putobject请求。
putobjectresult result = ossclient.putobject(putobjectrequest);
} catch (ossexception oe) {
system.out.println("caught an ossexception, which means your request made it to oss, "
+ "but was rejected with an error response for some reason.");
system.out.println("error message:" + oe.geterrormessage());
system.out.println("error code:" + oe.geterrorcode());
system.out.println("request id:" + oe.getrequestid());
system.out.println("host id:" + oe.gethostid());
} catch (clientexception ce) {
system.out.println("caught an clientexception, which means the client encountered "
+ "a serious internal problem while trying to communicate with oss, "
+ "such as not being able to access the network.");
system.out.println("error message:" + ce.getmessage());
} finally {
if (ossclient != null) {
ossclient.shutdown();
}
}
}
上传网络流
以下代码用于将网络流上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
public static void main(string[] args) throws exception {
// endpoint以华东1(杭州)为例,其它region请按实际情况填写。
string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量oss_access_key_id和oss_access_key_secret。
environmentvariablecredentialsprovider credentialsprovider = credentialsproviderfactory.newenvironmentvariablecredentialsprovider();
// 填写bucket名称,例如examplebucket。
string bucketname = "examplebucket";
// 填写object完整路径,完整路径中不能包含bucket名称,例如exampledir/exampleobject.txt。
string objectname = "exampledir/exampleobject.txt";
// 填写网络流地址。
string url = "https://www.aliyun.com/";
// 创建ossclient实例。
oss ossclient = new ossclientbuilder().build(endpoint, credentialsprovider);
try {
inputstream inputstream = new url(url).openstream();
// 创建putobjectrequest对象。
putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, inputstream);
// 创建putobject请求。
putobjectresult result = ossclient.putobject(putobjectrequest);
} catch (ossexception oe) {
system.out.println("caught an ossexception, which means your request made it to oss, "
+ "but was rejected with an error response for some reason.");
system.out.println("error message:" + oe.geterrormessage());
system.out.println("error code:" + oe.geterrorcode());
system.out.println("request id:" + oe.getrequestid());
system.out.println("host id:" + oe.gethostid());
} catch (clientexception ce) {
system.out.println("caught an clientexception, which means the client encountered "
+ "a serious internal problem while trying to communicate with oss, "
+ "such as not being able to access the network.");
system.out.println("error message:" + ce.getmessage());
} finally {
if (ossclient != null) {
ossclient.shutdown();
}
}
}
}
上传文件流
以下代码用于将文件流上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
public static void main(string[] args) throws exception {
// endpoint以华东1(杭州)为例,其它region请按实际情况填写。
string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量oss_access_key_id和oss_access_key_secret。
environmentvariablecredentialsprovider credentialsprovider = credentialsproviderfactory.newenvironmentvariablecredentialsprovider();
// 填写bucket名称,例如examplebucket。
string bucketname = "examplebucket";
// 填写object完整路径,完整路径中不能包含bucket名称,例如exampledir/exampleobject.txt。
string objectname = "exampledir/exampleobject.txt";
// 填写本地文件的完整路径,例如d:\\localpath\\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
string filepath= "d:\\localpath\\examplefile.txt";
// 创建ossclient实例。
oss ossclient = new ossclientbuilder().build(endpoint, credentialsprovider);
try {
inputstream inputstream = new fileinputstream(filepath);
// 创建putobjectrequest对象。
putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, inputstream);
// 创建putobject请求。
putobjectresult result = ossclient.putobject(putobjectrequest);
} catch (ossexception oe) {
system.out.println("caught an ossexception, which means your request made it to oss, "
+ "but was rejected with an error response for some reason.");
system.out.println("error message:" + oe.geterrormessage());
system.out.println("error code:" + oe.geterrorcode());
system.out.println("request id:" + oe.getrequestid());
system.out.println("host id:" + oe.gethostid());
} catch (clientexception ce) {
system.out.println("caught an clientexception, which means the client encountered "
+ "a serious internal problem while trying to communicate with oss, "
+ "such as not being able to access the network.");
system.out.println("error message:" + ce.getmessage());
} finally {
if (ossclient != null) {
ossclient.shutdown();
}
}
}
文件上传
使用文件上传,您可以将本地文件上传到oss文件。
以下代码用于将本地文件examplefile.txt上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
public static void main(string[] args) throws exception {
// endpoint以华东1(杭州)为例,其它region请按实际情况填写。
string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量oss_access_key_id和oss_access_key_secret。
environmentvariablecredentialsprovider credentialsprovider = credentialsproviderfactory.newenvironmentvariablecredentialsprovider();
// 填写bucket名称,例如examplebucket。
string bucketname = "examplebucket";
// 填写object完整路径,完整路径中不能包含bucket名称,例如exampledir/exampleobject.txt。
string objectname = "exampledir/exampleobject.txt";
// 填写本地文件的完整路径,例如d:\\localpath\\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
string filepath= "d:\\localpath\\examplefile.txt";
// 创建ossclient实例。
oss ossclient = new ossclientbuilder().build(endpoint, credentialsprovider);
try {
// 创建putobjectrequest对象。
putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, new file(filepath));
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
// objectmetadata metadata = new objectmetadata();
// metadata.setheader(ossheaders.oss_storage_class, storageclass.standard.tostring());
// metadata.setobjectacl(cannedaccesscontrollist.private);
// putobjectrequest.setmetadata(metadata);
// 上传文件。
putobjectresult result = ossclient.putobject(putobjectrequest);
} catch (ossexception oe) {
system.out.println("caught an ossexception, which means your request made it to oss, "
+ "but was rejected with an error response for some reason.");
system.out.println("error message:" + oe.geterrormessage());
system.out.println("error code:" + oe.geterrorcode());
system.out.println("request id:" + oe.getrequestid());
system.out.println("host id:" + oe.gethostid());
} catch (clientexception ce) {
system.out.println("caught an clientexception, which means the client encountered "
+ "a serious internal problem while trying to communicate with oss, "
+ "such as not being able to access the network.");
system.out.println("error message:" + ce.getmessage());
} finally {
if (ossclient != null) {
ossclient.shutdown();
}
}
}
发表评论