洛阳建设部官方网站,与安网站建设,高端网站建设合同,wordpress转程序阿里云官网#xff1a;阿里云-计算#xff0c;为了无法计算的价值
通过阿里云官网#xff0c;登录进入用户的界面#xff0c;在搜索框中输入OSS#xff0c;然后进入阿里云的对象存储OSS的控制台。#xff08;未开通的开通即可#xff09; 创建 Bucket
点击【Bucket 列…阿里云官网阿里云-计算为了无法计算的价值
通过阿里云官网登录进入用户的界面在搜索框中输入OSS然后进入阿里云的对象存储OSS的控制台。未开通的开通即可 创建 Bucket
点击【Bucket 列表】查看个人的Bucket。点击【创建 Bucket】创建新的 Bucket。其中必填项有
Bucket 名称唯一的就行地域选择一个近的就行例如华南1深圳存储类型选择【标准存储】读写权限选择【公共读】其他选项默认 点击【确定】即可创建。 上传文件
可以通过【文件管理】中【文件列表】进行上传文件 点击【上传文件】即可上传指定文件到OSS中。 选择上传文件步骤如图。除了上传文件外还可以上传文件夹。 点击【上传文件】后通过【任务列表】可以查看上传的文件情况。 之后可以在【文件列表】中查看上传的文件且可以点击【详情】查看文件的信息。
在【详情】中可以注意到文件有一个URL地址我们可以通过这个地址下载该文件。 配置RAM用户
如果想要在开发中进行操作阿里云OSS云存储的文件那么需要配置 RAM。配置的具体操作如下。
点击【账户头像】找到【AccessKey 管理】。 生成用户的【AccessKey ID】和【AccessKey Secret】。这里需要将其记住以便后面开发中使用。
定义OSS相关配置
sky:alioss:endpoint: oss-cn-hangzhou.aliyuncs.com根据自己情况填写access-key-id: *************根据自己情况填写access-key-secret: **********根据自己情况填写bucket-name: sky-take-out-zhangxi根据自己情况填写读取OSS配置
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefix sky.alioss)
Data
public class AliOssProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;}生成OSS工具类对象
import com.sky.properties.AliOssProperties;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 配置类用于创建AliOssUtil对象*/
Configuration
Slf4j
public class OssConfiguration {BeanConditionalOnMissingBeanpublic AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info(开始创建阿里云文件上传工具类对象{},aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}AliOssUtil.java如下
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;Data
AllArgsConstructor
Slf4j
public class AliOssUtil {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;/*** 文件上传** param bytes* param objectName* return*/public String upload(byte[] bytes, String objectName) {// 创建OSSClient实例。OSS ossClient new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建PutObject请求。ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));} 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();}}//文件访问路径规则 https://BucketName.Endpoint/ObjectNameStringBuilder stringBuilder new StringBuilder(https://);stringBuilder.append(bucketName).append(.).append(endpoint).append(/).append(objectName);log.info(文件上传到:{}, stringBuilder.toString());return stringBuilder.toString();}
}在server模块中定义文件上传接口
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;/*** 通用接口*/
RestController
RequestMapping(/admin/common)
Api(tags 通用接口)
Slf4j
public class CommonController {Autowiredprivate AliOssUtil aliOssUtil;/*** 文件上传* param file* return*/PostMapping(/upload)ApiOperation(文件上传)public ResultString upload(MultipartFile file){log.info(文件上传{},file);try {//原始文件名String originalFilename file.getOriginalFilename();//截取原始文件名的后缀 dfdfdf.pngString extension originalFilename.substring(originalFilename.lastIndexOf(.));//构造新文件名称String objectName UUID.randomUUID().toString() extension;//文件的请求路径String filePath aliOssUtil.upload(file.getBytes(), objectName);return Result.success(filePath);} catch (IOException e) {log.error(文件上传失败{}, e);}return Result.error(MessageConstant.UPLOAD_FAILED);}
}