先登录:
点击进入控制台
点击左上角导航栏按钮
搜索oss,点击进入
进入之后点击立即开通oss按钮,开通之后点击下图立即创建,弹出创建Bucket
填上Bucket名称,读写权限改为公共读。其他不变点击确定创建,完成之后点击进入Bucket
光标放到右上角头像,弹出AccessKey管理,点击进入,点击继续使用AccessKey
点击创建AccessKey,完成手机验证码验证
保存好AccessKey
左侧导航栏,点击sdk下载。找到java,点击查看小图标查看文档,再文档中心打开
找到java安装,将maven依赖坐标导入,pom.xml中。
- <dependency>
- <groupId>com.aliyun.ossgroupId>
- <artifactId>aliyun-sdk-ossartifactId>
- <version>3.15.1version>
- dependency>
- <dependency>
- <groupId>javax.xml.bindgroupId>
- <artifactId>jaxb-apiartifactId>
- <version>2.3.1version>
- dependency>
- <dependency>
- <groupId>javax.activationgroupId>
- <artifactId>activationartifactId>
- <version>1.1.1version>
- dependency>
-
- <dependency>
- <groupId>org.glassfish.jaxbgroupId>
- <artifactId>jaxb-runtimeartifactId>
- <version>2.3.3version>
- dependency>
找到java简单上传,将上传字符串这个代码复制过来
测试类中建一个demo类
改代码:---->
- /*endpoint是服务器的区域节点,将我们自己服务器的节点复制过来*/
- String endpoint = "https://oss-cn-beijing.aliyuncs.com";
- //注释掉自带的定义的环境变量,用我们自己的AccessKey定义
- //EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
- String ACCESS_KEY_ID="LTAI5tNGKYfsdhshhgs7GZ";
- String ACCESS_KEY_SECRET="UBMW01zF9AAtrhN7W4ZqBdTvyylD";
- /*我们的bucket名称,我们之前自己定义的,填过来*/
- String bucketName = "god-626";
- /*我们定义要存储的对象的名字*/
- String objectName = "123.png";
- /*credentialsProvider被我们注释掉了,改成ACCESS_KEY_ID,ACCESS_KEY_SECRET*/
- OSS ossClient = new OSSClientBuilder().build(endpoint,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
-
- try {
- // 填写字符串。
- String content = "Hello OSS,你好世界";
-
- // 创建PutObjectRequest对象。
- /*new 字符串输入流,改为new 文件输入流,加上我们定义的地址*/
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new FileInputStream("C:\\Users\\19545\\Desktop\\files\\123.jpg"));
完整代码:
- package com.lin.springboot01;
-
- import com.aliyun.oss.ClientException;
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.common.auth.*;
- import com.aliyun.oss.OSSClientBuilder;
- import com.aliyun.oss.OSSException;
- import com.aliyun.oss.model.PutObjectRequest;
- import com.aliyun.oss.model.PutObjectResult;
- import java.io.ByteArrayInputStream;
- import java.io.FileInputStream;
-
- public class Demo {
-
- public static void main(String[] args) throws Exception {
- // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
- /*endpoint是服务器的区域节点,将我们自己服务器的节点复制过来*/
- String endpoint = "https://oss-cn-beijing.aliyuncs.com";
- // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
- //注释掉自带的定义的环境变量,用我们自己的AccessKey定义
- //EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
- String ACCESS_KEY_ID="LTAI5tNGgdsfdstVc1GH7GZ";
- String ACCESS_KEY_SECRET="UBMWQ01zsgsfdgsdgfBdTvyUDylD";
- // 填写Bucket名称,例如examplebucket。
- /*我们的bucket名称,我们之前自己定义的,填过来*/
- String bucketName = "god-626";
- // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
- /*我们定义要存储的对象的名字*/
- String objectName = "123.png";
-
- // 创建OSSClient实例。
- /*credentialsProvider被我们注释掉了,改成ACCESS_KEY_ID,ACCESS_KEY_SECRET*/
- OSS ossClient = new OSSClientBuilder().build(endpoint,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
-
- try {
- // 填写字符串。
- String content = "Hello OSS,你好世界";
-
- // 创建PutObjectRequest对象。
- /*new 字符串输入流,改为new 文件输入流,加上我们定义的地址*/
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new FileInputStream("C:\\Users\\19545\\Desktop\\files\\123.jpg"));
-
- // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
- // 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();
- }
- }
- }
- }
完成,运行!