阿里云对象存储OSS上传照片(附源码)
Posted 谷哥的小弟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云对象存储OSS上传照片(附源码)相关的知识,希望对你有一定的参考价值。
版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
参考教程
- OSS官方文档:https://help.aliyun.com/document_detail/32007.html
- RAM访问控制:https://blog.csdn.net/lfdfhl/article/details/127156419
概述
在本教程中详细介绍阿里云对象存储OSS上传照片的详细步骤及其注意事项。
创建Maven项目
请在IDEA中创建Maven项目;图示如下:
添加依赖
请在项目中添加依赖;图示如下:
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
</dependency>
创建存储空间
存储空间是OSS的全局命名空间,相当于数据的容器,可以存储若干文件。
所以,你可以使用原有的Bucket;也可以通过代码新建Bucket;代码如下:
package com.cn;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Demo1
public static void main(String[] args) throws Exception
// 请按实际情况填写endpoint
String endpoint = "https://oss-cn-chengdu.aliyuncs.com";
// 建议创建并使用RAM用户并填写其accessKeyId和accessKeySecret
String accessKeyId = Config.AccessKeyID;
String accessKeySecret = Config.AccessKeySecret;
// 填写Bucket名称
String bucketName = "lfdfhl01";
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try
// 创建存储空间
ossClient.createBucket(bucketName);
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
// 关闭OSSClient实例
if (ossClient != null)
ossClient.shutdown();
图示如下:
程序运行完毕;图示如下:
在阿里云OSS管理控制台查看新增的Bucket;图示如下:
上传照片
在此,实现向Bucket上传照片的功能;代码如下:
package com.cn;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.File;
import java.io.FileInputStream;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Demo2
public static void main(String[] args) throws Exception
// 请按实际情况填写endpoint
String endpoint = "https://oss-cn-chengdu.aliyuncs.com";
// 建议创建并使用RAM用户并填写其accessKeyId和accessKeySecret
String accessKeyId = Config.AccessKeyID;
String accessKeySecret = Config.AccessKeySecret;
// 填写Bucket名称
String bucketName = "lfdfhl01";
// 待上传文件
File file = new File("D:\\\\测试资源\\\\beauty.jpg");
// 获取文件输入流
FileInputStream fileInputStream = new FileInputStream(file);
// 填写Object完整路径(即上传后保存的路径及文件名)
String objectName = file.getName();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try
// 上传文件
ossClient.putObject(bucketName, objectName, fileInputStream);
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
// 关闭OSSClient实例
if (ossClient != null)
ossClient.shutdown();
// 上传成功后拼接文件在服务器上的地址
String url = "https://" + bucketName + "."+"oss-cn-chengdu.aliyuncs.com" +"/"+objectName;
System.out.println("文件上传成功,url="+url);
操作成功后可在对应的Bucket中查看上传的图片;图示如下:
友情提示
Bucket的权限是“公共读”时可能导致外界的大量恶意访问造成流量过度消耗。所以,我们要有意识地检查是否将权限修改为了私有;图示如下:
当然,在开发过程中假若不需要考虑数据安全且便于测试时可将权限重新改为公共即可。
以上是关于阿里云对象存储OSS上传照片(附源码)的主要内容,如果未能解决你的问题,请参考以下文章