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.重启电脑

相关推荐
BXCQ_xuan11 小时前
阿里云CDN的源站配置:权重的详解
阿里云·云计算
Austindatabases20 小时前
给阿里云MongoDB 的感谢信 !!成本降低80%
数据库·mongodb·阿里云·云计算
阿里云云原生2 天前
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
数据库·阿里云·云计算
千叶真尹2 天前
基于阿里云DataWorks的物流履约时效离线分析
阿里云·云计算
zh73142 天前
支付宝沙盒模式商家转账经常出现 响应异常: 解包错误
前端·阿里云·php
Mr.小怪2 天前
自定义prometheus exporter实现监控阿里云RDS
阿里云·adb·prometheus
阿里云云原生2 天前
剑指大规模 AI 可观测,阿里云 Prometheus 2.0 应运而生
人工智能·阿里云·prometheus
国际云,接待2 天前
云计算的基础概论
服务器·人工智能·阿里云·云原生·云计算·腾讯云·aws
我不是帅戈3 天前
STM32+安信可Ai-WB2-12F连接阿里云物联网平台
stm32·物联网·阿里云·云计算·安信可
牧羊狼的狼3 天前
阿里云服务器-宝塔面板安装【保姆级教程】
运维·服务器·阿里云·宝塔