项目背景
SpringBoot
的项目,使用阿里云对象存储OSS
对项目中的文件进行存储,所需文件也会通过IDEA中由官方Demo改编而成的工具类作为接口,调用接口后上传
问题描述
使用阿里云OSS实现文件上传后,通过postman测试得到的url无法在浏览器中打开
原因分析
url
的拼接有问题
获取到的url如下
{"code": 0,"message": "操作成功","data": "https://finger-art.oss-cn-shanghai.aliyuncs.com96caa8xxx.png"
}
可以看到在 aliyun.com
后少了一个 "/"
,正确的路径应该是 https://finger-art.oss-cn-shanghai.aliyuncs.com/96caa8xxx.png
package cn.itcast.product.Utils;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
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.InputStream;public class ALiOssUtil {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。private static final String ENDPOINT = "https://oss-cn-shanghai.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。// EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();private static final String ACCESS_KEY_ID="xxx";private static final String ACCESS_KEY_SECRET="xxx";// 填写Bucket名称,例如examplebucket。private static final String BUCKET_NAME = "finger-art";public static String upLoadFile(String objectName, InputStream inputStream) throws Exception {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);String url="";try {// 填写字符串。String content = "Hello OSS,你好世界";// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName,inputStream);// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。// 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);url = "https://"+BUCKET_NAME+"."+ ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+ objectName;} 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();}}return url;}
}
在这里出现问题
解决方案
下面这条语句是截取了字符串 ENDPOINT
,从最后一个 "/"
开始截取,其结果应该为 oss-cn-shanghai.aliyuncs.com
,因此需要在这条语句后再加上一个 "/"
才能达到效果
ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1
正确语句如下
url = "https://"+BUCKET_NAME+"."+ ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+ objectName;
测试
成功下载,上传成功!