03_阿里云_配置OSS环境变量

关于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。

shell 复制代码
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

java 复制代码
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用户的访问密钥。

shell 复制代码
set OSS_ACCESS_KEY_ID=AccessKey ID
set OSS_ACCESS_KEY_SECRET=AccessKey Secret

3.执行以下命令以使更改生效。

shell 复制代码
setx OSS_ACCESS_KEY_ID "%OSS_ACCESS_KEY_ID%"
setx OSS_ACCESS_KEY_SECRET "%OSS_ACCESS_KEY_SECRET%"

4.执行以下命令验证环境变量配置。

shell 复制代码
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管理中获取)

5.最后确认创建环境变量

6.重启电脑

相关推荐
开发者导航10 小时前
【开发者导航】支持多存储方式的开源文件列表程序:OpenList
人工智能·学习·阿里云·信息可视化
熙客12 小时前
阿里云监控:SLS的使用
运维·阿里云·云原生·云计算
聚名网12 小时前
阿里云和聚名网的域名注册安全性如何?
阿里云·云计算
TG_yunshuguoji13 小时前
阿里云渠道商:哪些方法能给服务器加速?
服务器·阿里云·云计算
企鹅侠客14 小时前
阿里云ACK多个Service绑定单个SLB实践
阿里云·云计算·service·ack
D11_14 小时前
在阿里云CentOS服务器上使用Certbot为Nginx配置SSL证书
服务器·阿里云·centos
xiaogg36781 天前
阿里云k8s部署微服务yaml和Dockerfile文件脚本
阿里云·微服务·kubernetes
TG_yunshuguoji2 天前
阿里云代理商:什么是阿里云CDN配额?
阿里云·云计算
Adorable老犀牛2 天前
阿里云-ECS实例信息统计并发送统计报告到企业微信
python·阿里云·云计算·企业微信
代码or搬砖3 天前
文件上传阿里云OSS以及本地图片服务器搭建
服务器·阿里云·云计算