国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OSS對(duì)象存儲(chǔ)的簡(jiǎn)單實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了OSS對(duì)象存儲(chǔ)的簡(jiǎn)單實(shí)現(xiàn)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前提準(zhǔn)備好阿里云對(duì)象存儲(chǔ)的賬號(hào)->創(chuàng)建一個(gè)bucket(設(shè)置好訪問(wèn)權(quán)限)->創(chuàng)建用于上傳文件的子賬號(hào)得到accessKey和secretKey以及endpoint->sdk例子java簡(jiǎn)單上傳的例子測(cè)試

  1. 引入alicloud-oss對(duì)象純存儲(chǔ)相關(guān)的依賴(lài)

  1. 在application.yml中配置accessKey和secretKey即可

  1. 使用OssClient對(duì)象調(diào)用方法上傳即可

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
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 {
? ? ? ? //accessKey和secretKey以及endpoint在配置文件中指定的話這里就不用重復(fù)指定
        // Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云賬號(hào)AccessKey擁有所有API的訪問(wèn)權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM用戶進(jìn)行API訪問(wèn)或日常運(yùn)維,請(qǐng)登錄RAM控制臺(tái)創(chuàng)建RAM用戶。
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // 填寫(xiě)B(tài)ucket名稱(chēng),例如examplebucket。
        String bucketName = "examplebucket";
        // 填寫(xiě)Object完整路徑,完整路徑中不能包含Bucket名稱(chēng),例如exampledir/exampleobject.txt。
        String objectName = "exampledir/exampleobject.txt";
        // 填寫(xiě)本地文件的完整路徑,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路徑,則默認(rèn)從示例程序所屬項(xiàng)目對(duì)應(yīng)本地路徑中上傳文件流。
        String filePath= "D:\\localpath\\examplefile.txt";

        // 創(chuàng)建OSSClient實(shí)例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 創(chuàng)建PutObjectRequest對(duì)象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 設(shè)置該屬性可以返回response。如果不設(shè)置,則返回的response為空。
            putObjectRequest.setProcess("true");
            // 創(chuàng)建PutObject請(qǐng)求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            // 如果上傳成功,則返回200。
            System.out.println(result.getResponse().getStatusCode());
        } 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();
            }
        }
    }
} 

一,項(xiàng)目結(jié)合實(shí)例

先發(fā)送請(qǐng)求獲得policy 在將對(duì)象發(fā)送阿里云對(duì)象存儲(chǔ)服務(wù)器 減輕后端壓力

建立一個(gè)oss服務(wù)器

將oss相關(guān)配置寫(xiě)在配置文件中

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    application:
      name: gulimall-third-party
    alicloud:
      access-key: LTAI5t6G5JAZ9RB*********** # 修改為自己的
      secret-key: QhhibxJFoXR9qUs*********** # 修改為自己的
      oss:
        endpoint: oss-cn-************* # 修改為自己的
        bucket: test # 修改為自己的
server:
  port: 30000

編寫(xiě)controller前端請(qǐng)求這個(gè)controller獲得policy

package com.wcw.gulimall.third.party.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;

import com.wcw.gulimall.third.party.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName com.wcw.gulimall.third.party.controller.OssController
 * @Description:
 * @Author wcw
 * @Version V1.0 Created on :2023/3/2 17:25
 */
@RestController
public class OssController {
    @Autowired
    private OSS ossClient;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endPoint;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;

    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;

    @RequestMapping("/oss/policy")
    public R policy() {
        // 填寫(xiě)Host地址,格式為https://bucketname.endpoint。
        String host = "https://" + bucket + "." + endPoint;
        // 設(shè)置上傳回調(diào)URL,即回調(diào)服務(wù)器地址,用于處理應(yīng)用服務(wù)器與OSS之間的通信。OSS會(huì)在文件上傳完成后,把文件上傳信息通過(guò)此回調(diào)URL發(fā)送給應(yīng)用服務(wù)器。
//        String callbackUrl = "https://192.168.0.0:8888";
        // 設(shè)置上傳到OSS文件的前綴,可置空此項(xiàng)。置空后,文件將上傳至Bucket的根目錄下。
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String dir = format+"/";
        Map<String, String> respMap = new LinkedHashMap<String, String>();

        // 創(chuàng)建ossClient實(shí)例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap.put("accessId", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            // respMap.put("expire", formatISO8601Date(expiration));


        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        }
        return R.ok().put("data",respMap);
    }
}

返回下面的對(duì)象

{?policy: '',
 signature: '',
key: '',
ossaccessKeyId: '',
 dir: '',
 host: '',}

攜帶policy按照aliyun oss官方文檔發(fā)送即可文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-755725.html

<template> 
  <div>
    <el-upload
      action="http://gulimall-wcw.oss-cn-chengdu.aliyuncs.com"
      :data="dataObj"
      list-type="picture"
      :multiple="false" :show-file-list="showFileList"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview">
      <el-button size="small" type="primary">點(diǎn)擊上傳</el-button>
      <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過(guò)10MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>
  </div>
</template>
<script>
   import {policy} from './policy'
   import { getUUID } from '@/utils'

  export default {
    name: 'singleUpload',
    props: {
      value: String
    },
    computed: {
      imageUrl() {
        return this.value;
      },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== ''&& this.value!==undefined;
        },
        set: function (newValue) {
        }
      }
    },
    data() {
      return {
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: '',
          // callback:'',
        },
        dialogVisible: false
      };
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },
      beforeUpload(file) {
        
        let _self = this;
        return new Promise((resolve, reject) => {
          policy().then(response => {
            console.log("response:",response);
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessId;
            _self.dataObj.key = response.data.dir +getUUID()+'_${filename}';
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            console.log("_self.dataObj:",_self.dataObj);
            resolve(true)
          }).catch(err => {
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file) {
        console.log("上傳成功...")
        this.showFileList = true;
        this.fileList.pop();
        this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}",file.name) });
        this.emitInput(this.fileList[0].url);
      }
    }
  }
</script>
<style>

</style>


到了這里,關(guān)于OSS對(duì)象存儲(chǔ)的簡(jiǎn)單實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 阿里云對(duì)象存儲(chǔ)(OSS)服務(wù)

    阿里云對(duì)象存儲(chǔ)(OSS)服務(wù) 引入依賴(lài) 這里 aliyun-oss-spring-boot-starter 中默認(rèn)引入的 aliyun-java-sdk-core 是 3.4.0 版本,但是 aliyun-spring-boot-dependencies 中對(duì) aliyun-java-sdk-core 版本管理為:4.5.0,會(huì)導(dǎo)致版本沖突 所以排除 aliyun-oss-spring-boot-starter 默認(rèn)的 aliyun-java-sdk-core ,單獨(dú)引入 4.5.0 版

    2024年01月25日
    瀏覽(89)
  • 阿里云對(duì)象存儲(chǔ)OSS使用

    阿里云對(duì)象存儲(chǔ)OSS使用

    對(duì)象存儲(chǔ)服務(wù)(Object Storage Service,簡(jiǎn)稱(chēng) OSS)為您提供基于網(wǎng)絡(luò)的數(shù)據(jù)存取服務(wù)。使用 OSS,您可以通過(guò)網(wǎng)絡(luò)隨時(shí)存儲(chǔ)和調(diào)用包括文本、圖片、音頻和視頻等在內(nèi)的各種非結(jié)構(gòu)化數(shù)據(jù)文件。 對(duì)象存儲(chǔ)可以簡(jiǎn)單理解為用來(lái)存儲(chǔ)圖片、音頻、視頻等非結(jié)構(gòu)化數(shù)據(jù)的數(shù)據(jù)池。相對(duì)于主

    2024年02月11日
    瀏覽(88)
  • 2.阿里云對(duì)象存儲(chǔ)OSS

    ????????文件上傳,是指將本地圖片、視頻、音頻等文件上傳到服務(wù)器上,可以供其他用戶瀏覽或下載的過(guò)程。文件上傳在項(xiàng)目中應(yīng)用非常廣泛,我們經(jīng)常發(fā)抖音、發(fā)朋友圈都用到了文件上傳功能。 實(shí)現(xiàn)文件上傳服務(wù),需要有存儲(chǔ)的支持,解決方案有以下幾種: 存儲(chǔ)方式

    2024年02月12日
    瀏覽(89)
  • 淺談阿里云對(duì)象存儲(chǔ)OSS

    淺談阿里云對(duì)象存儲(chǔ)OSS

    OSS(即Object Storage Service)是一種提供海量、安全、低成本、高可靠的云存儲(chǔ)服務(wù),適合存放任意類(lèi)型的文件。容量和處理能力彈性擴(kuò)展,多種存儲(chǔ)類(lèi)型供選擇,全面優(yōu)化存儲(chǔ)成本,官方一點(diǎn)解釋就是對(duì)象存儲(chǔ)是一種使用HTTP API存儲(chǔ)和檢索非結(jié)構(gòu)化數(shù)據(jù)和元數(shù)據(jù)對(duì)象的工具。白

    2024年02月12日
    瀏覽(90)
  • 阿里云oss對(duì)象存儲(chǔ)的使用

    阿里云oss對(duì)象存儲(chǔ)的使用

    1.介紹 對(duì)象存儲(chǔ)服務(wù)(Object Storage Service,OSS)是一種海量、安全、低成本、高可靠的云存儲(chǔ) 服務(wù),適合存放任意類(lèi)型的文件。容量和處理能力彈性擴(kuò)展,多種存儲(chǔ)類(lèi)型供選擇,全面優(yōu) 化存儲(chǔ)成本。 2.使用步驟 ? 1)登錄阿里云:https://www.aliyun.com ? 2)開(kāi)通阿里云對(duì)象存儲(chǔ)服

    2024年01月17日
    瀏覽(88)
  • 阿里云對(duì)象存儲(chǔ)OSS怎么收費(fèi)?

    阿里云對(duì)象存儲(chǔ)OSS怎么收費(fèi)?

    阿里云對(duì)象存儲(chǔ)OSS收費(fèi)有兩種計(jì)費(fèi)模式,即包年包月和按量付費(fèi),包年包月是指購(gòu)買(mǎi)存儲(chǔ)包、流量包來(lái)抵扣OSS產(chǎn)生的存儲(chǔ)費(fèi)核流量費(fèi),OSS標(biāo)準(zhǔn)(LRS)存儲(chǔ)包100GB優(yōu)惠價(jià)33元、500GB存儲(chǔ)包半年162元、OSS存儲(chǔ)包40GB一年9元,OSS流量包100G 49元/月,阿里云百科來(lái)詳細(xì)說(shuō)下阿里云對(duì)象存儲(chǔ)

    2024年01月19日
    瀏覽(98)
  • 阿里云對(duì)象存儲(chǔ)OSS文件上傳

    阿里云對(duì)象存儲(chǔ)OSS文件上傳

    阿里云oss地址: 對(duì)象存儲(chǔ)OSS_云存儲(chǔ)服務(wù)_企業(yè)數(shù)據(jù)管理_存儲(chǔ)-阿里云 阿里云對(duì)象存儲(chǔ)OSS是一款海量、安全、低成本、高可靠的云存儲(chǔ)服務(wù),提供12個(gè)9的數(shù)據(jù)持久性,99.995%的數(shù)據(jù)可用性和多種存儲(chǔ)類(lèi)型,適用于數(shù)據(jù)湖存儲(chǔ),數(shù)據(jù)遷移,企業(yè)數(shù)據(jù)管理,數(shù)據(jù)處理等多種場(chǎng)景,可對(duì)

    2024年02月12日
    瀏覽(24)
  • SpringBoot集成-阿里云對(duì)象存儲(chǔ)OSS

    SpringBoot集成-阿里云對(duì)象存儲(chǔ)OSS

    阿里云對(duì)象存儲(chǔ) OSS (Object Storage Service),是一款海量、安全、低成本、高可靠的云存儲(chǔ)服務(wù)。使用 OSS,你可以通過(guò)網(wǎng)絡(luò)隨時(shí)存儲(chǔ)和調(diào)用包括文本、圖片、音頻和視頻等在內(nèi)的各種文件。 登錄阿里云后進(jìn)入阿里云控制臺(tái)首頁(yè)選擇 對(duì)象存儲(chǔ) OSS 服務(wù) 開(kāi)通服務(wù) 創(chuàng)建Bucket 填寫(xiě)

    2024年02月06日
    瀏覽(18)
  • 阿里云對(duì)象存儲(chǔ)服務(wù)OSS前后聯(lián)調(diào)

    阿里云對(duì)象存儲(chǔ)服務(wù)OSS前后聯(lián)調(diào)

    申明: 未經(jīng)許可,禁止以任何形式轉(zhuǎn)載,若要引用,請(qǐng)標(biāo)注鏈接地址 全文共計(jì)11577字,閱讀大概需要3分鐘 在分布式集群系統(tǒng)中,前端通過(guò)瀏覽器上傳圖片給服務(wù)器存儲(chǔ)時(shí)存在分庫(kù)分表的情況,這就涉及到 文件存儲(chǔ) 的情況,在高并發(fā)的情況下,考慮到服務(wù)器的性能和利用率

    2023年04月09日
    瀏覽(33)
  • 阿里云對(duì)象存儲(chǔ)OSS學(xué)習(xí)筆記4

    存儲(chǔ)類(lèi)型介紹: 文件存儲(chǔ):NAS 文件存儲(chǔ)、NFS。 塊存儲(chǔ):SAN iSCSI: 硬盤(pán)通過(guò)IP的方式提供給其他設(shè)備使用。 對(duì)象存儲(chǔ):OSS、CUS:我們創(chuàng)建了一個(gè)存儲(chǔ)池,我們的文件有文件本身、給文件生成元數(shù)據(jù)、文件唯一的ID。我們可以通過(guò)http和https來(lái)閱讀。存儲(chǔ)內(nèi)容:內(nèi)容不會(huì)變化的,例

    2024年02月06日
    瀏覽(96)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包