181人参与 • 2024-07-31 • Access
在黑马程序员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.
以下代码用于将文件流上传到目标存储空间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();
}
}
}
}
首次配置配置成功后需要重启电脑,环境变量的值才能够获取到,否则就会出现access key id should not be null or empty的问题
set oss_access_key_id=accesskey id
set oss_access_key_secret=accesskey secret
setx oss_access_key_id "%oss_access_key_id%"
setx oss_access_key_secret "%oss_access_key_secret%"
echo %oss_access_key_id%
echo %oss_access_key_secret%
变量名:oss_access_key_id
变量值:accesskey id
(阿里云中accesskey管理中获取)
变量名:oss_access_key_secret
变量值:accesskey secret
(阿里云中accesskey管理中获取)
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论