关于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管理中获取)
发表评论