阿里云官網(wǎng):阿里云-計(jì)算,為了無法計(jì)算的價(jià)值
通過阿里云官網(wǎng),登錄進(jìn)入用戶的界面,在搜索框中輸入OSS,然后進(jìn)入阿里云的對象存儲OSS的控制臺。(未開通的開通即可)
創(chuàng)建 Bucket
點(diǎn)擊【Bucket 列表】,查看個(gè)人的Bucket。點(diǎn)擊【創(chuàng)建 Bucket】,創(chuàng)建新的 Bucket。其中必填項(xiàng)有:
- Bucket 名稱:唯一的就行
- 地域:選擇一個(gè)近的就行,例如華南1(深圳)
- 存儲類型:選擇【標(biāo)準(zhǔn)存儲】
- 讀寫權(quán)限:選擇【公共讀】
- 其他選項(xiàng):默認(rèn)
點(diǎn)擊【確定】,即可創(chuàng)建。
上傳文件
可以通過【文件管理】中【文件列表】進(jìn)行上傳文件
點(diǎn)擊【上傳文件】,即可上傳指定文件到OSS中。
選擇上傳文件,步驟如圖。除了上傳文件外,還可以上傳文件夾。
點(diǎn)擊【上傳文件】后,通過【任務(wù)列表】可以查看上傳的文件情況。
之后,可以在【文件列表】中查看上傳的文件,且可以點(diǎn)擊【詳情】查看文件的信息。
在【詳情】中,可以注意到文件有一個(gè)URL地址,我們可以通過這個(gè)地址下載該文件。
配置RAM用戶
如果想要在開發(fā)中進(jìn)行操作阿里云OSS云存儲的文件,那么需要配置 RAM。配置的具體操作如下。
點(diǎn)擊【賬戶頭像】,找到【AccessKey 管理】。
文章來源:http://www.zghlxwxcb.cn/news/detail-731267.html
生成用戶的【AccessKey ID】和【AccessKey Secret】。這里需要將其記住,以便后面開發(fā)中使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-731267.html
定義OSS相關(guān)配置
sky:
alioss:
endpoint: oss-cn-hangzhou.aliyuncs.com(根據(jù)自己情況填寫)
access-key-id: *************(根據(jù)自己情況填寫)
access-key-secret: **********(根據(jù)自己情況填寫)
bucket-name: sky-take-out-zhangxi(根據(jù)自己情況填寫)
讀取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;
/**
* 配置類,用于創(chuàng)建AliOssUtil對象
*/
@Configuration
@Slf4j
public class OssConfiguration {
@Bean
@ConditionalOnMissingBean
public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
log.info("開始創(chuàng)建阿里云文件上傳工具類對象:{}",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) {
// 創(chuàng)建OSSClient實(shí)例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// 創(chuàng)建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();
}
}
//文件訪問路徑規(guī)則 https://BucketName.Endpoint/ObjectName
StringBuilder 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 {
@Autowired
private AliOssUtil aliOssUtil;
/**
* 文件上傳
* @param file
* @return
*/
@PostMapping("/upload")
@ApiOperation("文件上傳")
public Result<String> upload(MultipartFile file){
log.info("文件上傳:{}",file);
try {
//原始文件名
String originalFilename = file.getOriginalFilename();
//截取原始文件名的后綴 dfdfdf.png
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//構(gòu)造新文件名稱
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);
}
}
到了這里,關(guān)于阿里云的OSS云存儲的基本使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!