表单上传

更新时间:
一键部署
我的部署

OSS表单上传允许网页应用通过标准HTML表单直接将文件上传至OSS。这种方式下,在前端页面选择文件后,浏览器发起POST请求直接将文件传输到OSS服务器,而无需经过网站服务器中转,减轻了服务器的压力,提高了文件上传的效率和稳定性。

使用限制

通过表单上传的方式上传的Object大小不能超过5 GB。

使用场景

表单上传广泛应用于Web应用程序,包括但不限于以下几个方面:

  • 用户资料上传:注册账号时上传头像、身份证照片或其他身份验证材料。在个人中心修改信息时上传新的头像或背景图。

  • 文件分享与存储:在网盘服务、协同办公平台等通过表单上传各种格式的文件,如文档、图片、音频、视频等至云端进行存储和共享。

  • 内容创作与发布:在博客、论坛、问答社区等平台编写文章并通过表单上传图片、附件等作为内容补充。

  • 电商管理:商家在电商平台后台上传商品图片、详细描述文件、资质证书等;买家在购买过程中上传发票需求或其他证明材料。

  • 在线教育平台:学生在提交作业或项目时上传文档、PPT、视频等作业文件;教师在课程建设时上传教学资料、课件等。

  • 求职招聘网站:求职者上传简历、作品集等求职材料;企业发布职位时上传公司LOGO、招聘信息文件等。

  • 问卷调查与反馈:填写在线调查问卷时上传附加的证据文件或说明文档。

  • 软件开发协作:在代码托管平台如GitHub、GitLab等上传代码文件或项目文档。

方案概述

您可以在服务端生成PostObject所需的Post签名、PostPolicy等信息,然后客户端可以凭借这些信息,在一定的限制下不依赖OSS SDK直接上传文件。您可以借助服务端生成的PostPolicy限制客户端上传的文件,例如限制文件大小、文件类型。此方案适用于通过HTML表单上传的方式上传文件。需要注意的是,此方案不支持基于分片上传大文件、基于分片断点续传的场景。更多信息,请参见PostObject

服务端通过Post签名和Post Policy授权客户端上传文件到OSS的过程如下。

image
  1. 客户端向业务服务器请求Post签名和Post Policy等信息。

  2. 业务服务器生成并返回Post签名和Post Policy等信息给客户端。

  3. 客户端使用Post签名和Post Policy等信息调用PostObject通过HTML表单的方式上传文件到OSS。

  4. OSS返回成功响应给客户端。

操作步骤

  1. 服务端生成Post签名和Post Policy等信息。

    • 示例工程:postsignature.zip

    • 示例代码

      服务端生成Post签名和Post Policy等信息的示例代码如下:

      说明

      当前代码支持一键部署,您可以直接在函数计算FC中一键部署本代码。oss-upload-post-signature-app

      Python

      import os
      from hashlib import sha1 as sha
      import json
      import base64
      import hmac
      import datetime
      import time
      
      # 配置环境变量OSS_ACCESS_KEY_ID。
      access_key_id = os.environ.get('OSS_ACCESS_KEY_ID')
      # 配置环境变量OSS_ACCESS_KEY_SECRET。
      access_key_secret = os.environ.get('OSS_ACCESS_KEY_SECRET')
      # 将<YOUR_BUCKET>替换为Bucket名称。
      bucket = '<YOUR_BUCKET>'
      # host的格式为bucketname.endpoint。将<YOUR_BUCKET>替换为Bucket名称。将<YOUR_ENDPOINT>替换为OSS Endpoint,例如oss-cn-hangzhou.aliyuncs.com。
      host = 'https://<YOUR_BUCKET>.<YOUR_ENDPOINT>'
      # 指定上传到OSS的文件前缀。
      upload_dir = 'user-dir-prefix/'
      # 指定过期时间,单位为秒。
      expire_time = 3600
      
      
      def generate_expiration(seconds):
          """
          通过指定有效的时长(秒)生成过期时间。
          :param seconds: 有效时长(秒)。
          :return: ISO8601 时间字符串,如:"2014-12-01T12:00:00.000Z"。
          """
          now = int(time.time())
          expiration_time = now + seconds
          gmt = datetime.datetime.utcfromtimestamp(expiration_time).isoformat()
          gmt += 'Z'
          return gmt
      
      
      def generate_signature(access_key_secret, expiration, conditions, policy_extra_props=None):
          """
          生成签名字符串Signature。
          :param access_key_secret: 有权限访问目标BucketAccessKeySecret。
          :param expiration: 签名过期时间,按照ISO8601标准表示,并需要使用UTC时间,格式为yyyy-MM-ddTHH:mm:ssZ。示例值:"2014-12-01T12:00:00.000Z"。
          :param conditions: 策略条件,用于限制上传表单时允许设置的值。
          :param policy_extra_props: 额外的policy参数,后续如果policy新增参数支持,可以在通过dict传入额外的参数。
          :return: signature,签名字符串。
          """
          policy_dict = {
              'expiration': expiration,
              'conditions': conditions
          }
          if policy_extra_props is not None:
              policy_dict.update(policy_extra_props)
          policy = json.dumps(policy_dict).strip()
          policy_encode = base64.b64encode(policy.encode())
          h = hmac.new(access_key_secret.encode(), policy_encode, sha)
          sign_result = base64.b64encode(h.digest()).strip()
          return sign_result.decode()
      
      def generate_upload_params():
          policy = {
              # 有效期。
              "expiration": generate_expiration(expire_time),
              # 约束条件。
              "conditions": [
                  # 未指定success_action_redirect时,上传成功后的返回状态码,默认为 204。
                  ["eq", "$success_action_status", "200"],
                  # 表单域的值必须以指定前缀开始。例如指定key的值以user/user1开始,则可以写为["starts-with", "$key", "user/user1"]。
                  ["starts-with", "$key", upload_dir],
                  # 限制上传Object的最小和最大允许大小,单位为字节。
                  ["content-length-range", 1, 1000000],
                  # 限制上传的文件为指定的图片类型
                  ["in", "$content-type", ["image/jpg", "image/png"]]
              ]
          }
          signature = generate_signature(access_key_secret, policy.get('expiration'), policy.get('conditions'))
          response = {
              'policy': base64.b64encode(json.dumps(policy).encode('utf-8')).decode(),
              'ossAccessKeyId': access_key_id,
              'signature': signature,
              'host': host,
              'dir': upload_dir
              # 可以在这里再自行追加其他参数
          }
          return json.dumps(response)
      

      Java

      import com.aliyun.help.demo.uploading_to_oss_directly_postsignature.config.OssConfig;
      import com.aliyun.oss.ClientException;
      import com.aliyun.oss.OSS;
      import com.aliyun.oss.OSSException;
      import com.aliyun.oss.common.utils.BinaryUtil;
      import com.aliyun.oss.model.MatchMode;
      import com.aliyun.oss.model.PolicyConditions;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.ResponseBody;
      import org.codehaus.jettison.json.JSONObject;
      import java.util.Date;
      import com.aliyun.oss.OSSClientBuilder;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Bean;
      import javax.annotation.PreDestroy;
      
      @Controller
      public class PostSignatureController {
          @Autowired
          private OSS ossClient;
      
          @Autowired
          private OssConfig ossConfig;
      
          @GetMapping("/get_post_signature_for_oss_upload")
          @ResponseBody
          public String generatePostSignature() {
              JSONObject response = new JSONObject();
              try {
                  long expireEndTime = System.currentTimeMillis() + ossConfig.getExpireTime() * 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, ossConfig.getDir());
                  String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
                  byte[] binaryData = postPolicy.getBytes("utf-8");
                  String encodedPolicy = BinaryUtil.toBase64String(binaryData);
                  String postSignature = ossClient.calculatePostSignature(postPolicy);
                  response.put("ossAccessKeyId", ossConfig.getAccessKeyId());
                  response.put("policy", encodedPolicy);
                  response.put("signature", postSignature);
                  response.put("dir", ossConfig.getDir());
                  response.put("host", ossConfig.getHost());
              } 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("HTTP Status Code: " + oe.getRawResponseError()); 
              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();
              }
              return response.toString();
          }
        }
      }
      
      @Configuration
      public class OssConfig {
          /**
           * 将 <YOUR-ENDPOINT> 替换为 Endpoint,例如 oss-cn-hangzhou.aliyuncs.com
           */
          private String endpoint = "<YOUR-ENDPOINT>";
          /**
           * 将 <YOUR-BUCKET> 替换为 Bucket 名称
           */
          private String bucket = "<YOUR-BUCKET>";
          /**
           * 指定上传到 OSS 的文件前缀
           */
          private String dir = "user-dir-prefix/";
          /**
           * 指定过期时间,单位为秒
           */
          private long expireTime = 3600;
          /**
           * 构造 host
           */
          private String host = "http://" + bucket + "." + endpoint;
          /**
           * 通过环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 设置 accessKeyId
           */
          private String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
          /**
           * 通过环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET 设置 accessKeySecret
           */
          private String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
      
          private OSS ossClient;
          @Bean
          public OSS getOssClient() {
              ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
              return ossClient;
          }
          @Bean
          public String getHost() {
              return host;
          }
          @Bean
          public String getAccessKeyId() {
              return accessKeyId;
          }
          @Bean
          public long getExpireTime() {
              return expireTime;
          }
          @Bean
          public String getDir() {
              return dir;
          }
      
          @PreDestroy
          public void onDestroy() {
              ossClient.shutdown();
          }
      }
      
      

      Go

      package main
      
      import (
          "crypto/hmac"
          "crypto/sha1"
          "encoding/base64"
          "encoding/json"
          "fmt"
          "io"
          "net/http"
          "os"
          "time"
      )
      
      var (
          // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID。
          accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
          // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_SECRET。
          accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
          // host的格式为bucketname.endpoint。将${your-bucket}替换为Bucket名称。将${your-endpoint}替换为OSS Endpoint,例如oss-cn-hangzhou.aliyuncs.com。
          host = "http://${your-bucket}.${your-endpoint}"
          // 指定上传到OSS的文件前缀。
          uploadDir = "user-dir-prefix/"
          // 指定过期时间,单位为秒。
          expireTime = int64(3600)
      )
      
      type ConfigStruct struct {
          Expiration string     `json:"expiration"`
          Conditions [][]string `json:"conditions"`
      }
      type PolicyToken struct {
          AccessKeyId string `json:"ossAccessKeyId"`
          Host        string `json:"host"`
          Signature   string `json:"signature"`
          Policy      string `json:"policy"`
          Directory   string `json:"dir"`
      }
      
      func getGMTISO8601(expireEnd int64) string {
          return time.Unix(expireEnd, 0).UTC().Format("2006-01-02T15:04:05Z")
      }
      func getPolicyToken() string {
          now := time.Now().Unix()
          expireEnd := now + expireTime
          tokenExpire := getGMTISO8601(expireEnd)
          var config ConfigStruct
          config.Expiration = tokenExpire
          var condition []string
          condition = append(condition, "starts-with")
          condition = append(condition, "$key")
          condition = append(condition, uploadDir)
          config.Conditions = append(config.Conditions, condition)
          result, err := json.Marshal(config)
          if err != nil {
          fmt.Println("callback json err:", err)
          return ""
          }
          encodedResult := base64.StdEncoding.EncodeToString(result)
          h := hmac.New(sha1.New, []byte(accessKeySecret))
          io.WriteString(h, encodedResult)
          signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
          policyToken := PolicyToken{
          AccessKeyId: accessKeyId,
          Host:        host,
          Signature:   signedStr,
          Policy:      encodedResult,
          Directory:   uploadDir,
          }
          response, err := json.Marshal(policyToken)
          if err != nil {
          fmt.Println("json err:", err)
          return ""
          }
          return string(response)
      }
      func handler(w http.ResponseWriter, r *http.Request) {
          if r.URL.Path == "/" {
          http.ServeFile(w, r, "templates/index.html")
          return
          } else if r.URL.Path == "/get_post_signature_for_oss_upload" {
          policyToken := getPolicyToken()
          w.Header().Set("Content-Type", "application/json")
          w.Write([]byte(policyToken))
          return
          }
          http.NotFound(w, r)
      }
      func main() {
          http.HandleFunc("/", handler)
          http.ListenAndServe(":8080", nil)
      }
      

      PHP

      <?php
      function gmt_iso8601($time)
      {
          return str_replace('+00:00', '.000Z', gmdate('c', $time));
      }
      
      // 从环境变量中获取访问凭证。运行本示例代码之前,请确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。
      $accessKeyId = getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
      $accessKeySecret = getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
      // $host的格式为<YOUR-BUCKET>.<YOUR-ENDPOINT>',请替换为您的真实信息。
      $host = 'http://<YOUR-BUCKET>.<YOUR-ENDPOINT>';
      // 用户上传文件时指定的前缀。
      $dir = 'user-dir-prefix/';          
      
      $now = time();
      //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。
      $expire = 30;  
      $end = $now + $expire;
      $expiration = gmt_iso8601($end);
      
      //最大文件大小.用户可以自己设置。
      $condition = array(0 => 'content-length-range', 1 => 0, 2 => 1048576000);
      $conditions[] = $condition;
      
      // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。
      $start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
      $conditions[] = $start;
      
      
      $arr = array('expiration' => $expiration, 'conditions' => $conditions);
      $policy = json_encode($arr);
      $base64_policy = base64_encode($policy);
      $string_to_sign = $base64_policy;
      $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $accessKeySecret, true));
      
      $response = array();
      $response['ossAccessKeyId'] = $accessKeyId;
      $response['host'] = $host;
      $response['policy'] = $base64_policy;
      $response['signature'] = $signature;
      $response['dir'] = $dir;  
      echo json_encode($response);
      

      Node.js

      const express = require("express");
      const { Buffer } = require("buffer");
      const OSS = require("ali-oss");
      const app = express();
      const path = require("path");
      const config = {
        // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID。
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
        // 将<YOUR-BUCKET>替换为Bucket名称。
        bucket: "<YOUR-BUCKET>",
        // 指定上传到OSS的文件前缀。
        dir: "prefix/",
      };
      
      app.use(express.static(path.join(__dirname, "templates")));
      
      app.get("/get_post_signature_for_oss_upload", async (req, res) => {
        const client = new OSS(config);
        const date = new Date();
        // 设置签名的有效期,单位为秒。
        date.setSeconds(date.getSeconds() + 3600);
        const policy = {
          expiration: date.toISOString(),
          conditions: [
            // 设置上传文件的大小限制。
            ["content-length-range", 0, 1048576000],
            // 限制可上传的Bucket。
            { bucket: client.options.bucket },
          ],
        };
        const formData = await client.calculatePostSignature(policy);
        const host = `http://${config.bucket}.${
          (await client.getBucketLocation()).location
        }.aliyuncs.com`.toString();
        const params = {
          policy: formData.policy,
          signature: formData.Signature,
          ossAccessKeyId: formData.OSSAccessKeyId,
          host,
          dir: config.dir,
        };
        res.json(params);
      });
      
      app.get(/^(.+)*\.(html|js)$/i, async (req, res) => {
        res.sendFile(path.join(__dirname, "./templates", req.originalUrl));
      });
      
      app.listen(8000, () => {
        console.log("http://127.0.0.1:8000");
      });
      

      Ruby

      require 'sinatra'
      require 'base64'
      require 'open-uri'
      require 'cgi'
      require 'openssl'
      require 'json'
      require 'sinatra/reloader'
      require 'sinatra/content_for'
      
      # 设置public文件夹路径为当前文件夹下的templates文件夹
      set :public_folder, File.dirname(__FILE__) + '/templates'
      
      # 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID。
      $access_key_id = ENV['ALIBABA_CLOUD_ACCESS_ID']
      # 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_SECRET。
      $access_key_secret = ENV['ALIBABA_CLOUD_ACCESS_SECRET']
      
      # $host的格式为<bucketname>.<endpoint>,请替换为您的真实信息。
      $host = 'http://<bucketname>.<endpoint>';
      
      # 用户上传文件时指定的前缀。
      $upload_dir = 'user-dir-prefix/'
      # 过期时间,单位为秒。
      $expire_time = 30
      $server_ip = "0.0.0.0"
      $server_port = 8000
      
      if ARGV.length == 1 
        $server_port = ARGV[0]
      elsif ARGV.length == 2
        $server_ip = ARGV[0]
        $server_port = ARGV[1]
      end
      
      puts "App server is running on: http://#{$server_ip}:#{$server_port}"
      
      def hash_to_jason(source_hash)
        jason_string = source_hash.to_json;    
      
        jason_string.gsub!("\":[", "\": [")
        jason_string.gsub!("\",\"", "\", \"")
        jason_string.gsub!("],\"", "], \"")
        jason_string.gsub!("\":\"", "\": \"")
      
        jason_string
      end
      
      
      def get_token()
        expire_syncpoint = Time.now.to_i + $expire_time
        expire = Time.at(expire_syncpoint).utc.iso8601()
        response.headers['expire'] = expire
        policy_dict = {}
        condition_arrary = Array.new
        array_item = Array.new
        array_item.push('starts-with')
        array_item.push('$key')
        array_item.push($upload_dir)
        condition_arrary.push(array_item)
        policy_dict["conditions"] = condition_arrary
        policy_dict["expiration"] = expire
        policy = hash_to_jason(policy_dict)
        policy_encode = Base64.strict_encode64(policy).chomp;
        h = OpenSSL::HMAC.digest('sha1', $access_key_secret, policy_encode)
        hs = Digest::MD5.hexdigest(h)
        sign_result = Base64.strict_encode64(h).strip()
        token_dict = {}
        token_dict['ossAccessKeyId'] = $access_key_id
        token_dict['host'] = $host
        token_dict['policy'] = policy_encode
        token_dict['signature'] = sign_result 
        token_dict['expire'] = expire_syncpoint
        token_dict['dir'] = $upload_dir
        result = hash_to_jason(token_dict)
        result
      end
      
      set :bind, $server_ip
      set :port, $server_port
      
      get '/get_post_signature_for_oss_upload' do
        token = get_token()
        puts "Token: #{token}"
        token
      end
      
      get '/*' do
        puts "********************* GET "
        send_file File.join(settings.public_folder, 'index.html')
      end
      
      end
      
      if ARGV.length == 1 
        $server_port = ARGV[0]
      elsif ARGV.length == 2
        $server_ip = ARGV[0]
        $server_port = ARGV[1]
      end
      
      $server_ip = "0.0.0.0"
      $server_port = 8000
      
      puts "App server is running on: http://#{$server_ip}:#{$server_port}"
      
      set :bind, $server_ip
      set :port, $server_port
      
      get '/get_sts_token_for_oss_upload' do
        token = get_sts_token_for_oss_upload()
        response = {
          "AccessKeyId" => token["Credentials"]["AccessKeyId"],
          "AccessKeySecret" => token["Credentials"]["AccessKeySecret"],
          "SecurityToken" => token["Credentials"]["SecurityToken"]
        }
        response.to_json
      end
      
      get '/*' do
        puts "********************* GET "
        send_file File.join(settings.public_folder, 'index.html')
      end
      

      C#

      using Microsoft.AspNetCore.Builder;
      using Microsoft.Extensions.DependencyInjection;
      using Microsoft.AspNetCore.Http;
      using System.IO;
      using System.Collections.Generic;
      using System;
      using System.Globalization;
      using System.Text;
      using System.Security.Cryptography;
      using Newtonsoft.Json;
      using Microsoft.AspNetCore.Http.Extensions;
      using Microsoft.AspNetCore.Mvc;
      using Microsoft.Extensions.Logging;
      
      namespace YourNamespace
      {
          public class Program
          {
              private ILogger<Program> _logger;
              // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID。
              public string AccessKeyId { get; set; } = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
              // 配置环境变量ALIBABA_CLOUD_ACCESS_KEY_SECRET。
              public string AccessKeySecret { get; set; } = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
              // host的格式为bucketname.endpoint。将<YOUR-BUCKET>替换为Bucket名称。将<YOUR-ENDPOINT>替换为OSS Endpoint,例如oss-cn-hangzhou.aliyuncs.com。
              public string Host { get; set; } = "<YOUR-BUCKET>.<YOUR-ENDPOINT>";
              // 指定上传到OSS的文件前缀。
              public string UploadDir { get; set; } = "user-dir-prefix/";
              // 指定过期时间,单位为秒。
              public int ExpireTime { get; set; } = 3600;
              public class PolicyConfig
              {
                  public string expiration { get; set; }
                  public List<List<object>> conditions { get; set; }
              }
              public class PolicyToken
              {
                  public string Accessid { get; set; }
                  public string Policy { get; set; }
                  public string Signature { get; set; }
                  public string Dir { get; set; }
                  public string Host { get; set; }
                  public string Expire { get; set; }
              }
              public static void Main(string[] args)
              {
                  var builder = WebApplication.CreateBuilder(args);
                  var app = builder.Build();
      
                  builder.Logging.AddConsole();
                  var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
      
                  app.UseStaticFiles(); 
      
                  app.MapGet("/", async (context) =>
                  {
                      var filePath = Path.Combine(Directory.GetCurrentDirectory(), "templates/index.html");
                      var htmlContent = await File.ReadAllTextAsync(filePath);
                      await context.Response.WriteAsync(htmlContent);
                      logger.LogInformation("GET request to root path");
                  });
      
                  app.MapGet("/get_post_signature_for_oss_upload", async (context) =>
                  {
                      var program = new Program(logger);
                      var token = program.GetPolicyToken();
      
                      logger.LogInformation($"Token: {token}");
      
                      context.Response.ContentType = "application/json";
                      await context.Response.WriteAsync(token);
                  });
      
                  app.Run();
              }
      
              public Program(ILogger<Program> logger)
              {
                  _logger = logger;
              }
      
              private string ToUnixTime(DateTime dateTime)
              {
                  return ((DateTimeOffset)dateTime).ToUnixTimeSeconds().ToString();
              }
      
              private string GetPolicyToken()
              {
                  var expireDateTime = DateTime.Now.AddSeconds(ExpireTime);
                  var config = new PolicyConfig
                  {
                      expiration = FormatIso8601Date(expireDateTime),
                      conditions = new List<List<object>>()
                  };
                  config.conditions.Add(new List<object>
                  {
                      "content-length-range", 0, 1048576000
                  });
                  var policy = JsonConvert.SerializeObject(config);
                  var policyBase64 = EncodeBase64("utf-8", policy);
                  var signature = ComputeSignature(AccessKeySecret, policyBase64);
                  var policyToken = new PolicyToken
                  {
                      Accessid = AccessKeyId,
                      Host = Host,
                      Policy = policyBase64,
                      Signature = signature,
                      Expire = ToUnixTime(expireDateTime),
                      Dir = UploadDir
                  };
                  return JsonConvert.SerializeObject(policyToken);
              }
      
              private string FormatIso8601Date(DateTime dtime)
              {
                  return dtime.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'",
                                          CultureInfo.CurrentCulture);
              }
      
              private string EncodeBase64(string codeType, string code)
              {
                  string encode = "";
                  byte[] bytes = Encoding.GetEncoding(codeType).GetBytes(code);
                  try
                  {
                      encode = Convert.ToBase64String(bytes);
                  }
                  catch
                  {
                      encode = code;
                  }
                  return encode;
              }
      
              private string ComputeSignature(string key, string data)
              {
                  using (var algorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)))
                  {
                      return Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(data)));
                  }
              }
          }
      }
      
  2. 客户端使用Post签名和Post Policy等信息调用PostObject通过HTML表单的方式上传文件到OSS。

    js为例:

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>上传文件到OSS</title>
    </head>
    <body>
      <div class="container">
        <form>
          <div class="mb-3">
            <label for="file" class="form-label">选择文件</label>
            <input type="file" class="form-control" id="file" name="file" required>
          </div>
          <button type="submit" class="btn btn-primary">上传</button>
        </form>
      </div>
      <script type="text/javascript">
          const form = document.querySelector('form');
          const fileInput = document.querySelector('#file');
          form.addEventListener('submit', (event) => {
            event.preventDefault();
            let file = fileInput.files[0];
            let filename = fileInput.files[0].name;
            fetch('/get_post_signature_for_oss_upload', { method: 'GET' })
              .then(response => response.json())
              .then(data => {
                const formData = new FormData();
                formData.append('name',filename);
                formData.append('policy', data.policy);
                formData.append('OSSAccessKeyId', data.ossAccessKeyId);
                formData.append('success_action_status', '200');
                formData.append('signature', data.signature);
                formData.append('key', data.dir + filename);
                // file必须为最后一个表单域,除file以外的其他表单域无顺序要求。
                formData.append('file', file);
                fetch(data.host, { method: 'POST', body: formData},).then((res) => {
                  console.log(res);
                  alert('文件已上传');
                });
              })
              .catch(error => {
                console.log('Error occurred while getting OSS upload parameters:', error);
              });
          });
      </script>
    </body>
    </html>
    

注意事项

数据安全

防止覆盖同名Object

上传时默认会覆盖同名Object,您可以选择以下任意方式防止Object被意外覆盖。

  • 开启版本控制功能

    开启版本控制功能后,被覆盖的Object会以历史版本的形式保存下来。您可以随时恢复历史版本Object。更多信息,请参见版本控制介绍

  • 在上传请求中携带禁止覆盖同名Object的参数

    在上传请求的Header中携带参数x-oss-forbid-overwrite,并指定其值为true。当您上传的ObjectOSS中存在同名Object时,该Object会上传失败,并返回FileAlreadyExists错误。当不携带此参数或此参数的值为false时,同名Object会被覆盖。

授权上传

  • 为了防止第三方用户未经授权在您的Bucket中上传数据,OSS提供了BucketObject级别的访问权限控制。更多信息,请参见访问控制

  • 使用签名URL授权第三方用户上传指定文件。签名URL允许第三方用户在没有安全凭证或者授权的情况下进行上传操作。第三方用户使用签名URL上传文件后,OSS将在指定的Bucket生成该文件。使用文件URL上传文件

降低PUT类请求费用

如果要上传的文件数量较多,直接指定上传的文件类型为深度冷归档类型会造成较高的PUT类请求费用。建议您先将文件的存储类型指定为标准存储进行上传,然后通过生命周期规则将其转储为深度冷归档类型,从而降低PUT类请求费用。

避免影响OSS-HDFS服务

为避免影响OSS-HDFS服务的正常使用或者引发数据丢失的风险,在开通了OSS-HDFS服务的Bucket中,禁止以非OSS-HDFS服务提供的方式在OSS-HDFS的数据存储目录.dlsdata/中上传Object。

上传性能调优

如果您在上传大量Object时,在命名上使用了顺序前缀(如时间戳或字母顺序),可能会出现大量Object索引集中存储于存储空间中某个特定分区的情况,进而导致请求速率下降。建议您在上传大量Object时,不要使用顺序前缀的Object名称,而是将顺序前缀改为随机性前缀。具体操作,请参见OSS性能与扩展性最佳实践