关于aliyunOSS文件上传的系统变量配置
问题引出
在黑马程序员2023新版JavaWeb开发教程教程
中,P148Day11-04. 案例-文件上传-阿里云OSS-准备
到P150Day11-06. 案例-文件上传-阿里云OSS-集成
阿里云给的参考代码已经更新了,需要配置阿里云的用户变量(Windows的用户变量)才能继续使用。
问题描述:
关于aliyunOSS文件上传出现Access key id should not be null or empty的问题
从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
Exception in thread "main" com.aliyun.oss.common.auth.InvalidCredentialsException: Access key id should not be null or empty.
aliyunOSS上传文件流官方示例代码
以下代码用于将文件流上传到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
官方示例:https://help.aliyun.com/zh/oss/developer-reference/java/?spm=a2c4g.11186623.0.0.6a097713R2kfVB
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.FileInputStream;
import java.io.InputStream;public class Demo {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();}}}
}
方案1:使用shell命令创建环境变量
首次配置配置成功后需要重启电脑,环境变量的值才能够获取到,否则就会出现Access key id should not be null or empty的问题
1.win+R输入cmd,回车,打开命令行。
2.执行以下命令配置RAM用户的访问密钥。
set OSS_ACCESS_KEY_ID=AccessKey ID
set OSS_ACCESS_KEY_SECRET=AccessKey Secret
3.执行以下命令以使更改生效。
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%"
setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%"
4.执行以下命令验证环境变量配置。
echo %OSS_ACCESS_KEY_ID%
echo %OSS_ACCESS_KEY_SECRET%
5.示例
6.重启电脑
方案2:使用视图工具添加环境变量
1.在设置中搜索环境变量
2.点新建用户变量
3.新建用户变量
变量名:OSS_ACCESS_KEY_ID
变量值:AccessKey ID
(阿里云中AccessKey管理中获取)
4.新建用户变量
变量名:OSS_ACCESS_KEY_SECRET
变量值:AccessKey Secret
(阿里云中AccessKey管理中获取)