{"id":357,"date":"2023-03-21T16:51:47","date_gmt":"2023-03-21T08:51:47","guid":{"rendered":"https:\/\/www.muxuetianyin.cn\/?p=357"},"modified":"2024-04-14T23:29:15","modified_gmt":"2024-04-14T15:29:15","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e7%9b%b4%e4%bc%a0obs","status":"publish","type":"post","link":"https:\/\/www.muxuetianyin.cn\/?p=357","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u76f4\u4f20OBS"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"section1\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n\n\n\n<ul id=\"obs_05_2000__ul114835468311\">\n<li>\u8ba1\u7b97\u7b7e\u540d\u65f6\u4f9d\u8d56\u5f15\u7528\u201ccrypto-js\u201d\u53ca\u201cjs-base64\u201d\u4e24\u4e2a\u5f00\u6e90\u7ec4\u4ef6\uff0c\u56e0\u6b64\u9700\u8981\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u9879\u76ee\u4e2d\u8bbe\u7f6e\u4f7f\u7528NPM\u6a21\u5757\u3002<\/li>\n\n\n\n<li>\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u7f16\u8bd1\u65f6\uff0c\u5982\u679c\u5728\u5f15\u5165\u201ccrypto-js\u201d\u5305\u65f6\u51fa\u73b0\u201cMaximum call stack size exceed\u201d\u62a5\u9519\uff0c\u8bf7\u5347\u7ea7\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u5ba2\u6237\u7aef\u81f3\u6700\u65b0\u7248\u672c\u3002<\/li>\n\n\n\n<li>\u4e0a\u4f20\u8fc7\u7a0b\u4e2d\u8fd4\u56de405\u65f6\uff0c\u8bf7\u68c0\u67e5\u6307\u5b9a\u7684endpoint\u662f\u5426\u4e3a\u5bf9\u5e94\u4e0a\u4f20\u6876\u7684\u6876\u57df\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"section2\">\u64cd\u4f5c\u6b65\u9aa4<\/h4>\n\n\n\n<ol id=\"obs_05_2000__ol7140112573211\">\n<li><strong>\u8bbe\u7f6e\u6876\u7684\u8de8\u57df\u8bbf\u95ee\u6743\u9650\u3002<\/strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u57fa\u4e8eBrowerJS\u8fdb\u884c\u5f00\u53d1\uff0c\u53d7\u540c\u6e90\u5b89\u5168\u7b56\u7565\u7684\u8981\u6c42\uff0c\u4e0d\u540c\u57df\u95f4\u7684\u7f51\u7ad9\u811a\u672c\u548c\u5185\u5bb9\u5982\u9700\u4ea4\u4e92\uff0c\u9700\u8981\u914d\u7f6e\u8de8\u57df\u8d44\u6e90\u5171\u4eab\uff08CORS\uff09\u89c4\u8303\u3002\u534e\u4e3a\u4e91\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1OBS\u652f\u6301CORS\u89c4\u8303\uff0c\u5141\u8bb8\u8de8\u57df\u8bbf\u95eeOBS\u4e2d\u7684\u8d44\u6e90\uff0c\u5177\u4f53\u914d\u7f6e\u6b65\u9aa4\u8bf7\u53c2\u89c1<a href=\"https:\/\/support.huaweicloud.com\/usermanual-obs\/zh-cn_topic_0066036542.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u914d\u7f6e\u8de8\u57df\u8d44\u6e90\u5171\u4eab<\/a>\u3002CORS\u89c4\u5219\u914d\u7f6e\u9879\u5efa\u8bae\uff1a<\/li>\n<\/ol>\n\n\n\n<p><a href=\"https:\/\/support.huaweicloud.com\/bestpractice-obs\/obs_05_2000.html\">\u5c0f\u7a0b\u5e8f\u76f4\u4f20OBS_\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1 OBS_\u6700\u4f73\u5b9e\u8df5_\u534e\u4e3a\u4e91 (huaweicloud.com)<\/a><\/p>\n\n\n\n<p>\u4f7f\u7528\u828b\u5934\u76f4\u63a5\u4e0a\u4f20\u6587\u4ef6\u5230\u534e\u4e3aOBS\uff0c\u53ef\u4ee5\u4f7f\u7528@vivo-miniprogram\/ Huawei &#8211; OBS -sdk\u5305\u3002\u6b64\u5305\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684API\uff0c\u7528\u4e8e\u4e0a\u4f20\u6587\u4ef6\u5230\u534e\u4e3aOBS\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4f7f\u7528npm\u5b89\u88c5\u5305:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install @vivo-miniprogram\/huawei-obs-sdk\n<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5728\u60a8\u7684Taro\u4ee3\u7801\u4e2d\u5bfc\u5165\u5305\uff0c\u5e76\u4f7f\u7528uploadFile\u65b9\u6cd5\u5c06\u60a8\u7684\u6587\u4ef6\u4e0a\u4f20\u5230\u534e\u4e3aOBS\u3002\u4f8b\u5b50:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { uploadFile } from '@vivo-miniprogram\/huawei-obs-sdk';\n\n\/\/ Replace these values with your own\nconst accessKeyId = 'your-access-key-id';\nconst secretAccessKey = 'your-secret-access-key';\nconst bucketName = 'your-bucket-name';\nconst objectName = 'your-object-name';\nconst filePath = 'path\/to\/your\/file';\n\n\/\/ Upload the file to Huawei OBS\nuploadFile({\n  accessKeyId,\n  secretAccessKey,\n  bucketName,\n  objectName,\n  filePath,\n}).then(() =&gt; {\n  console.log('File uploaded successfully');\n}).catch((error) =&gt; {\n  console.error('Error uploading file:', error);\n});\n<\/code><\/pre>\n\n\n\n<p>\u8bf7\u786e\u4fdd\u5c06accessKeyId\u3001secretAccessKey\u3001bucketName\u3001objectName\u548cfilePath\u503c\u66ff\u6362\u4e3a\u60a8\u81ea\u5df1\u7684\u503c\u3002\u60a8\u53ef\u4ee5\u5728\u534e\u4e3aOBS\u63a7\u5236\u53f0\u4e2d\u627e\u5230\u60a8\u7684\u63a5\u5165\u5bc6\u94a5ID\u548c\u79d8\u5bc6\u63a5\u5165\u5bc6\u94a5\u3002<\/p>\n\n\n\n<p>\u53c2\u8003<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wx.uploadFile({\n  url: 'https:\/\/your-obs-server',\n  filePath: 'path\/to\/local\/file',\n  name: 'file',\n  formData: {\n    key: 'path\/to\/file',\n    policy: 'your-policy',\n    signature: 'your-signature',\n  },\n  success: (res) =&gt; {\n    console.log('Upload success:', res);\n  },\n  fail: (err) =&gt; {\n    console.error('Upload error:', err);\n  },\n});\n\u5b9e\u9645\u6848\u4f8b\u8981\u4f20\u7684\u53c2\u6570\u6bd4\u8f83\u591a\nConfiguration.js\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ \u6307\u5b9aOBS\u670d\u52a1\u76f8\u5173\u4fe1\u606f\uff1aAK\uff0cSK\uff0cEndPoint\nclass Configuration{\n\n  constructor(){\n\n    this.AccessKeyId = '' \/\/AK\n\n    this.SecretKey = ''  \/\/SK\n\n    this.SecurityToken = ''  \/\/ securitytoken\n\n    this.EndPoint = ''  \/\/ \u8bbf\u95ee\u8def\u5f84 \u5c0f\u7a0b\u5e8f \u8c03\u7528 \u524d\u7f00 \u8981\u52a0 \u6876\u540d\n\n    this.bucket = ''   \/\/\u6876\u540d\n\n    this.Expiration = ''  \/\/ token \u6709\u6548\u671f\n\n    this.FileUploadPath = ''  \/\/ \u4e0a\u4f20\u5230 \u4e91\u4e0a\u7684 \u6587\u4ef6\u5939\u5730\u5740\n\n    this.DomainUrl = ''  \/\/\u56fe\u7247\u8bbf\u95ee\u8def\u5f84\n\n\n\n  }\n\n  setConfig({ accessKeyId,secretKey,securityToken,bucket,endpoint, expiresAt, fileUploadPath,domainUrl}){\n\n\n    this.AccessKeyId = accessKeyId\n\n    this.SecretKey = secretKey\n\n    this.SecurityToken = securityToken\n\n    this.bucket = bucket\n\n    const _endpoint = `https:\/\/${bucket}.${endpoint}`\n\n    this.EndPoint = _endpoint\n\n    this.Expiration = expiresAt\n\n    this.FileUploadPath = fileUploadPath\n\n    this.DomainUrl = domainUrl\n\n\n  }\n\n}\n\n\nmodule.exports = new Configuration();<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const config = require('.\/Configuration.js');\nconst getPolicyEncode = require('.\/getPolicy.js');\nconst getSignature = require('.\/GetSignature.js');\nconst uuid = require('.\/uuid')\n\nconst setObsConfig = params =&gt; {\n\n  config.setConfig(params)\n\n}\n\nconst OBSupload = function (filePath,fileName){\n\n  if(!filePath){\n    wx.showToast({\n      title: '\u6587\u4ef6\u8def\u5f84\u4e0d\u80fd\u4e3a\u7a7a',\n      icon: 'none',\n    });\n    return false\n  }\n\n\n return wxUpload(filePath,fileName)\n\n}\n\n\n\nconst wxUpload = (filePath,fileName)=&gt;{\n\n  const pathSplit =  filePath.split('.')\n\n  const photoType = pathSplit&#91;pathSplit.length-1]\n\n  const contentType = `image\/${photoType}`\n\n\n  let _fileName = fileName ? fileName : `picture-mini`\n\n  \/\/\u91cd\u540d\u4f1a\u88ab\u8986\u76d6\uff0c\u8fd9\u91cc\u52a0\u4e0b \u65f6\u95f4\u6233\n  _fileName = `${_fileName}-${uuid()}.${photoType}`\n\n     \/\/\u8bbe\u5b9apolicy\u5185\u5bb9\n  const OBSPolicy = {\n    \"expiration\": config.Expiration, \/\/token\u6709\u6548\u671f\n    \"conditions\": &#91;\n      {\"bucket\": config.bucket},        \/\/Bucket name\n      {'content-type':contentType},\n      {\"x-obs-acl\": \"public-read\"},\n      { 'key': `${config.FileUploadPath}${_fileName}`},\n    ]\n  }\n\n  const policyEncoded = getPolicyEncode(OBSPolicy);                    \/\/\u8ba1\u7b97policy\u7f16\u7801\u503c\n  const signature = getSignature(policyEncoded, config.SecretKey);     \/\/\u8ba1\u7b97signature\n\n  return new Promise((revose,reject)=&gt;{\n\n    wx.uploadFile({\n      url: config.EndPoint,\n      filePath: filePath,\n      name: 'file',\n      header: {\n        'content-type': 'multipart\/form-data; boundary=-9431149156168',\n      },\n      formData: {\n        'AccessKeyID': config.AccessKeyId,\n        'policy': policyEncoded,\n        'signature': signature,\n        \/\/ \"x-obs-security-token\": config.SecurityToken,\n        'content-type':contentType,\n        'x-obs-acl': 'public-read',\n        'key': `${config.FileUploadPath}${_fileName}`,\n      },\n      success: function(res){\n\n        if(res.statusCode=='204'){\n          \/\/ http\u56fe\u7247\u5730\u5740 \u5982\uff1a\"https:\/\/img\/20210630\/picture-mini-1625018844736.png\"\n\n          const domainUrl = config.DomainUrl\n          const _chart = domainUrl.endsWith('\/') ?  '' : '\/'\n          const url = `${config.DomainUrl}${_chart}${config.FileUploadPath}${_fileName}`\n          \/\/\u76f8\u5bf9\u8def\u5f84 \u5982:'\/dev\/zj-mall\/img\/20210624\/321321-1624539083010.png'\n          const path = `${config.FileUploadPath}${_fileName}`\n\n          revose({\n            url ,\n            path,\n            domainUrl,\n          })\n        }\n        else{\n          reject(res)\n        }\n      },\n      fail: function(e){\n        reject(e)\n      }\n    })\n\n\n  })\n\n}\n\n\n\n\nmodule.exports = {\n\n  OBSupload,\n\n  setObsConfig\n\n\n}\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u4f20\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Taro from '@tarojs\/taro'\nimport OBSWx from '@\/lib\/OBS'\nimport dayjs from 'dayjs'\nimport utc from 'dayjs\/plugin\/utc'\n\ndayjs.extend(utc)\n\nconst config = {\n  AccessKeyId: '',\n  SecretKey: '',\n  EndPoint: '',\n  Bucket: '',\n}\n\nconst prefixPath = 'upload\/mp\/'\n\nexport default async function obsUploadFile(filePath: string) {\n  \/\/ const { filePath } = options\n\n  if (!filePath) {\n    Taro.showToast({\n      title: '\u65e0\u6548\u76ee\u5f55',\n      icon: 'error',\n    })\n    throw new Error('Invalid filePath')\n  }\n\n  OBSWx.setObsConfig({\n    accessKeyId: config.AccessKeyId,\n    secretKey: config.SecretKey,\n    \/\/ \u6876\u540d\n    bucket: config.Bucket,\n    \/\/ \u8bbf\u95ee\u8def\u5f84 \u5c0f\u7a0b\u5e8f \u8c03\u7528 \u524d\u7f00 \u8981\u52a0 \u6876\u540d  \u8fd4\u56de\u62fc\u88c5\u597d\u7684 \u5982 \uff1a`https:\/\/${bucket}.${endpoint}`\n    endpoint: config.EndPoint,\n    \/\/  token \u6709\u6548\u671f\n    expiresAt: dayjs\n      .utc()\n      .add(30, 'm')\n      .format(),\n    fileUploadPath: prefixPath, \/\/ \u4e0a\u4f20\u5230 \u4e91\u4e0a\u7684 \u6587\u4ef6\u5939\u5730\u5740\n    domainUrl: `https:\/\/${config.Bucket}.${config.EndPoint}\/`, \/\/ \u56fe\u7247\u8bbf\u95ee\u8def\u5f84\n  })\n\n  const result = await OBSWx.OBSupload(filePath)\n\n  return result\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u610f\u4e8b\u9879 \u64cd\u4f5c\u6b65\u9aa4 \u5c0f\u7a0b\u5e8f\u76f4\u4f20OBS_\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1 OBS_\u6700\u4f73\u5b9e\u8df5_\u534e\u4e3a\u4e91 (huaweicloud.com [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/357"}],"collection":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357"}],"version-history":[{"count":6,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/415"}],"wp:attachment":[{"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muxuetianyin.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}