打开网站:阿里云OSS:对象存储 OSS_云存储服务_企业数据管理_存储-阿里云 (aliyun.com)
记住在创建的时候记得保存AccessKeySecret,现在已经不被允许再次查看
进入之后点击创建:
<!--阿里云oss1-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
打开SDK下载,再点开SDK示例
点开:
找到Java快速入门:
就可以找到以下代码用于创建存储空间。直接拿这个用也是可以的
package com.example;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以华北2(北京)为例,其它Region请按实际情况填写。
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
// 阿里云账号
String accessKeyId = "LTAI5tCvdmnowKuE7AxwQDnV";
String accessKeySecret = "lbIroYZDWWdb39KcnUwTYKaRh7Iirf";
// 填写Bucket名称,例如web-tlias-file-start。
String bucketName = "web-tlias-file-start";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如web-tlias-file-start/web-tlias-file-start.txt。
String objectName = "1.jpg";
//制定本地路径
String filePath = "C:\\staticImage\\2.jpg";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObjectRequest对象。
ossClient.putObject(bucketName, objectName, inputStream);
} 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();
}
}
}
}