Tous les produits
Search
Centre de documentation

Object Storage Service:Charger un object à l'aide d'une URL pré-signée (SDK Python V2)

Dernière mise à jour :Aug 27, 2026

Le propriétaire du fichier génère une URL pré-signée pour la méthode PUT, incluant des en-têtes de requête spécifiques et des métadonnées personnalisées.

import argparse
import requests
import alibabacloud_oss_v2 as oss

from datetime import datetime, timedelta

# Create a command-line argument parser and describe the script's purpose: to generate a presigned PUT request URL for an object (Presign Put Object).
parser = argparse.ArgumentParser(description="presign put object sample")

# Define command-line arguments, including the required region, bucket name, endpoint, and object key.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    # Parse command-line arguments to get user-provided values.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the SDK's default settings and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client with the above configuration to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to generate a presigned PUT request for the specified object.
    pre_result = client.presign(oss.PutObjectRequest(
        bucket=args.bucket,  # Bucket name
        key=args.key,  # Object key
        content_type='text/plain;charset=utf8',  # Specify the content type.
        storage_class='Standard',  # Specify the storage class.
        metadata={
            'key1': 'value1',   # Specify the metadata.
            'key2': 'value2'    # Specify the metadata.
        }
    ),expires=timedelta(seconds=3600)) # Set the expiration time in seconds. Here, it is set to 3600 seconds.

    # Print the method, expiration time, and URL of the presigned request to confirm the validity of the link.
    print(f'method: {pre_result.method},'
          f' expiration: {pre_result.expiration.strftime("%Y-%m-%dT%H:%M:%S.000Z")},'
          f' url: {pre_result.url}'
          )

    # Print the signed header information of the presigned request. This information will be included in the HTTP headers when the actual request is sent.
    for key, value in pre_result.signed_headers.items():
        print(f'signed headers key: {key}, signed headers value: {value}')

# When this script is executed directly, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # Script entry point. The program flow starts here.
  • Un autre utilisateur télécharge le fichier en utilisant l’URL pré-signée pour la méthode PUT.

    curl

    curl -X PUT \
         -H "Content-Type: text/plain;charset=utf8" \
         -H "x-oss-storage-class: Standard" \
         -H "x-oss-meta-key1: value1" \
         -H "x-oss-meta-key2: value2" \
         -T "C:\\Users\\demo.txt" \
         "https://exampleobject.oss-cn-hangzhou.aliyuncs.com/exampleobject.txt?x-oss-date=20241112T083238Z&x-oss-expires=3599&x-oss-signature-version=OSS4-HMAC-SHA256&x-oss-credential=LTAI****************%2F20241112%2Fcn-hangzhou%2Foss%2Faliyun_v4_request&x-oss-signature=ed5a******************************************************"

    Java

    package com.aliyun.sample;
    
    import com.aliyun.oss.internal.OSSHeaders;
    import com.aliyun.oss.model.StorageClass;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.entity.FileEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import java.io.*;
    import java.net.URL;
    import java.util.*;
    
    public class SignUrlUpload {
        public static void main(String[] args) throws Throwable {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
    
            // Replace <signedUrl> with the authorized URL.
            URL signedUrl = new URL("<signedUrl>");
    
            // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
            String pathName = "C:\\Users\\demo.txt";
    
            // Set the request headers. The request header information must be the same as the information used when the URL was generated.
            Map<String, String> headers = new HashMap<String, String>();
            //Specify the storage class of the object.
            headers.put(OSSHeaders.STORAGE_CLASS, StorageClass.Standard.toString());
            //Specify the ContentType.
            headers.put(OSSHeaders.CONTENT_TYPE, "text/plain;charset=utf8");
    
            // Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
            Map<String, String> userMetadata = new HashMap<String, String>();
            userMetadata.put("key1","value1");
            userMetadata.put("key2","value2");
    
            try {
                HttpPut put = new HttpPut(signedUrl.toString());
                System.out.println(put);
                HttpEntity entity = new FileEntity(new File(pathName));
                put.setEntity(entity);
                // If you set header parameters, such as user metadata and storage class, when you generate the presigned URL, you must also send these parameters to the server when you use the presigned URL to upload the file. A signature error is reported if the parameters sent to the server are inconsistent with the signed parameters.
                for(Map.Entry<String, String> header: headers.entrySet()){
                    put.addHeader(header.getKey(), header.getValue());
                }
                for(Map.Entry<String, String> meta: userMetadata.entrySet()){
                    // If you use userMeta, the SDK internally adds the 'x-oss-meta-' prefix. When you use other methods to generate a presigned URL for an upload, you must also add the 'x-oss-meta-' prefix to the userMeta.
                    put.addHeader("x-oss-meta-"+meta.getKey(), meta.getValue());
                }
    
                httpClient = HttpClients.createDefault();
    
                response = httpClient.execute(put);
    
                System.out.println("Status code returned for the upload:"+response.getStatusLine().getStatusCode());
                if(response.getStatusLine().getStatusCode() == 200){
                    System.out.println("Upload successful using network library.");
                }
                System.out.println(response.toString());
            } catch (Exception e){
                e.printStackTrace();
            } finally {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            }
        }
    }

    Go

    package main
    
    import (
    	"bytes"
    	"fmt"
    	"io/ioutil"
    	"net/http"
    	"os"
    )
    
    func uploadFile(signedUrl string, filePath string, headers map[string]string, metadata map[string]string) error {
    	// Open the file.
    	file, err := os.Open(filePath)
    	if err != nil {
    		return err
    	}
    	defer file.Close()
    
    	// Read the file content.
    	fileBytes, err := ioutil.ReadAll(file)
    	if err != nil {
    		return err
    	}
    
    	// Create a request.
    	req, err := http.NewRequest("PUT", signedUrl, bytes.NewBuffer(fileBytes))
    	if err != nil {
    		return err
    	}
    
    	// Set the request headers.
    	for key, value := range headers {
    		req.Header.Set(key, value)
    	}
    
    	// Set the custom user metadata.
    	for key, value := range metadata {
    		req.Header.Set(fmt.Sprintf("x-oss-meta-%s", key), value)
    	}
    
    	// Send the request.
    	client := &http.Client{}
    	resp, err := client.Do(req)
    	if err != nil {
    		return err
    	}
    	defer resp.Body.Close()
    
    	// Process the response.
    	fmt.Printf("Status code returned for the upload: %d\n", resp.StatusCode)
    	if resp.StatusCode == 200 {
    		fmt.Println("Upload successful using network library.")
    	} else {
    		fmt.Println("Upload failed.")
    	}
    	body, _ := ioutil.ReadAll(resp.Body)
    	fmt.Println(string(body))
    
    	return nil
    }
    
    func main() {
    	// Replace <signedUrl> with the authorized URL.
    	signedUrl := "<signedUrl>"
    
    	// Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
    	filePath := "C:\\Users\\demo.txt"
    
    	// Set the request headers. The request header information must be the same as the information used when the URL was generated.
    	headers := map[string]string{
    		"Content-Type": "text/plain;charset=utf8",
    		"x-oss-storage-class": "Standard",
    	}
    
    	// Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
    	metadata := map[string]string{
    		"key1": "value1",
    		"key2": "value2",
    	}
    
    	err := uploadFile(signedUrl, filePath, headers, metadata)
    	if err != nil {
    		fmt.Printf("An error occurred: %v\n", err)
    	}
    }
    

    Python

    import requests
    from requests.auth import HTTPBasicAuth
    import os
    
    def upload_file(signed_url, file_path, headers=None, metadata=None):
        """
        Use a presigned URL to upload a file to OSS.
    
        :param signed_url: The presigned URL.
        :param file_path: The full path of the file to upload.
        :param headers: Optional. Custom HTTP headers.
        :param metadata: Optional. Custom metadata.
        :return: None
        """
        if not headers:
            headers = {}
        if not metadata:
            metadata = {}
    
        # Update headers and add the metadata prefix.
        for key, value in metadata.items():
            headers[f'x-oss-meta-{key}'] = value
    
        try:
            with open(file_path, 'rb') as file:
                response = requests.put(signed_url, data=file, headers=headers)
                print(f"Status code returned for the upload: {response.status_code}")
                if response.status_code == 200:
                    print("Upload successful using network library.")
                else:
                    print("Upload failed.")
                print(response.text)
        except Exception as e:
            print(f"An error occurred: {e}")
    
    if __name__ == "__main__":
        # Replace <signedUrl> with the authorized URL.
        signed_url = "<signedUrl>"
       
        # Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the directory where the script is located by default.
        file_path = "C:\\Users\\demo.txt"
    
        # Set the request headers. The request header information must be the same as the information used when the URL was generated.
        headers = {
             "Content-Type": "text/plain;charset=utf8",
             "x-oss-storage-class": "Standard"
        }
    
        # Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
        metadata = {
             "key1": "value1",
             "key2": "value2"
        }
    
        upload_file(signed_url, file_path, headers, metadata)
    

    Node.js

    const fs = require('fs');
    const axios = require('axios');
    
    async function uploadFile(signedUrl, filePath, headers = {}, metadata = {}) {
        try {
            // Update headers and add the metadata prefix.
            for (const [key, value] of Object.entries(metadata)) {
                headers[`x-oss-meta-${key}`] = value;
            }
    
            // Read the file stream.
            const fileStream = fs.createReadStream(filePath);
    
            // Send a PUT request.
            const response = await axios.put(signedUrl, fileStream, {
                headers: headers
            });
    
            console.log(`Status code returned for the upload: ${response.status}`);
            if (response.status === 200) {
                console.log("Upload successful using network library.");
            } else {
                console.log("Upload failed.");
            }
            console.log(response.data);
        } catch (error) {
            console.error(`An error occurred: ${error.message}`);
        }
    }
    
    // Main function.
    (async () => {
        // Replace <signedUrl> with the authorized URL.
        const signedUrl = "<signedUrl>";
    
        // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the directory where the script is located by default.
        const filePath = "C:\\Users\\demo.txt";
    
        // Set the request headers. The request header information must be the same as the information used when the URL was generated.
        const headers = {
             "Content-Type": "text/plain;charset=utf8",
             "x-oss-storage-class": "Standard"
        };
    
        // Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
        const metadata = {
             "key1": "value1",
             "key2": "value2"
        };
    
        await uploadFile(signedUrl, filePath, headers, metadata);
    })();
    

    Browser.js

    Important

    Si vous rencontrez une erreur de discordance de signature 403 lors du téléchargement d’un fichier avec Browser.js, cela est généralement dû au fait que le navigateur ajoute automatiquement l’en-tête de requête Content-Type, qui n’a pas été inclus lors de la génération de l’URL pré-signée. Cette incompatibilité entraîne un échec de la vérification de la signature. Pour résoudre ce problème, vous devez spécifier l’en-tête de requête Content-Type lors de la génération de l’URL pré-signée.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>File Upload Example</title>
    </head>
    <body>
        <h1>File Upload Example (Java SDK)</h1>
    
        <input type="file" id="fileInput" />
        <button id="uploadButton">Upload File</button>
    
        <script>
            // Replace this with the actual presigned URL.
            const signedUrl = "<signedUrl>"; 
    
            document.getElementById('uploadButton').addEventListener('click', async () => {
                const fileInput = document.getElementById('fileInput');
                const file = fileInput.files[0];
    
                if (file) {
                    try {
                        await upload(file, signedUrl);
                    } catch (error) {
                        console.error('Error during upload:', error);
                        alert('Upload failed: ' + error.message);
                    }
                } else {
                    alert('Please select a file to upload.');
                }
            });
    
            const upload = async (file, presignedUrl) => {
                const headers = {
                    "Content-Type": "text/plain;charset=utf8",
                    'x-oss-storage-class': 'Standard',
                    'x-oss-meta-key1': 'value1',
                    'x-oss-meta-key2': 'value2'
                };
    
                const response = await fetch(presignedUrl, {
                    method: 'PUT',
                    headers: headers,
                    body: file
                });
    
                if (!response.ok) {
                    throw new Error(`Upload failed, status: ${response.status}`);
                }
    
                alert('File uploaded successfully');
                console.log('File uploaded successfully');
            };
        </script>
    </body>
    </html>

    C#

    using System.Net.Http.Headers;
    
    // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
    var filePath = "C:\\Users\\demo.txt";
    // Replace <signedUrl> with the authorized URL.
    var presignedUrl = "<signedUrl>";
    
    // Create an HTTP client and open the local file stream.
    using var httpClient = new HttpClient(); 
    using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    using var content = new StreamContent(fileStream);
                
    // Create a PUT request.
    var request = new HttpRequestMessage(HttpMethod.Put, presignedUrl);
    request.Content = content;
    
    // If you set header parameters, such as user metadata and storage class, when you generate the presigned URL, you must also send these parameters to the server when you use the presigned URL to upload the file. A signature error is reported if the parameters sent to the server are inconsistent with the signed parameters.
    // Set the request headers. The request header information must be the same as the information used when the URL was generated.
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain") { CharSet = "utf8" };  // Specify the ContentType.       
    // Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
    request.Content.Headers.Add("x-oss-meta-key1", "value1");
    request.Content.Headers.Add("x-oss-meta-key2", "value2");
    // Specify the storage class of the object.
    request.Content.Headers.Add("x-oss-storage-class", "Standard");
    
    // Print the request headers.
    Console.WriteLine("Request headers:");
    foreach (var header in request.Content.Headers)
    {
        Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
    }
    
    // Send the request.
    var response = await httpClient.SendAsync(request);
    
    // Process the response.
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine($"Upload successful! Status code: {response.StatusCode}");
        Console.WriteLine("Response headers:");
        foreach (var header in response.Headers)
        {
            Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
        }
    }
    else
    {
        string responseContent = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"Upload failed! Status code: {response.StatusCode}");
        Console.WriteLine("Response content: " + responseContent);
    }

    C++

    #include <iostream>
    #include <fstream>
    #include <curl/curl.h>
    #include <map>
    #include <string>
    
    // Callback function to handle the HTTP response.
    size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
        size_t totalSize = size * nmemb;
        output->append((char*)contents, totalSize);
        return totalSize;
    }
    
    void uploadFile(const std::string& signedUrl, const std::string& filePath, const std::map<std::string, std::string>& headers, const std::map<std::string, std::string>& metadata) {
        CURL* curl;
        CURLcode res;
        std::string readBuffer;
    
        curl_global_init(CURL_GLOBAL_DEFAULT);
        curl = curl_easy_init();
    
        if (curl) {
            // Set the URL.
            curl_easy_setopt(curl, CURLOPT_URL, signedUrl.c_str());
    
            // Set the request method to PUT.
            curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    
            // Open the file.
            FILE* file = fopen(filePath.c_str(), "rb");
            if (!file) {
                std::cerr << "Unable to open file: " << filePath << std::endl;
                return;
            }
    
            // Set the file size.
            fseek(file, 0, SEEK_END);
            long fileSize = ftell(file);
            rewind(file);
    
            // Set the file read callback.
            curl_easy_setopt(curl, CURLOPT_READDATA, file);
            curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
    
            // Set the request headers.
            struct curl_slist* chunk = nullptr;
            for (const auto& header : headers) {
                std::string headerStr = header.first + ": " + header.second;
                chunk = curl_slist_append(chunk, headerStr.c_str());
            }
            for (const auto& meta : metadata) {
                std::string metaStr = "x-oss-meta-" + meta.first + ": " + meta.second;
                chunk = curl_slist_append(chunk, metaStr.c_str());
            }
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    
            // Set the response handling callback.
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    
            // Execute the request.
            res = curl_easy_perform(curl);
    
            // Check the response.
            if (res != CURLE_OK) {
                std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
            } else {
                long responseCode;
                curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
                std::cout << "Status code returned for the upload: " << responseCode << std::endl;
                if (responseCode == 200) {
                    std::cout << "Upload successful using network library." << std::endl;
                } else {
                    std::cout << "Upload failed." << std::endl;
                }
                std::cout << readBuffer << std::endl;
            }
    
            // Clean up.
            fclose(file);
            curl_slist_free_all(chunk);
            curl_easy_cleanup(curl);
        }
    
        curl_global_cleanup();
    }
    
    int main() {
        // Replace <signedUrl> with the authorized URL.
        std::string signedUrl = "<signedUrl>";
    
        // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
        std::string filePath = "C:\\Users\\demo.txt";
    
        // Set the request headers. The request header information must be the same as the information used when the URL was generated.
        std::map<std::string, std::string> headers = {
             {"Content-Type", "text/plain;charset=utf8"},
             {"x-oss-storage-class", "Standard"}
        };
    
        // Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
        std::map<std::string, std::string> metadata = {
             {"key1", "value1"},
             {"key2", "value2"}
        };
    
        uploadFile(signedUrl, filePath, headers, metadata);
    
        return 0;
    }
    

    Android

    import android.os.AsyncTask;
    import android.util.Log;
    
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class SignUrlUploadActivity extends AppCompatActivity {
    
        private static final String TAG = "SignUrlUploadActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Replace <signedUrl> with the authorized URL.
            String signedUrl = "<signedUrl>";
    
            // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
            String pathName = "/storage/emulated/0/demo.txt";
    
            // Set the request headers. The request header information must be the same as the information used when the URL was generated.
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "text/plain;charset=utf8");
            headers.put("x-oss-storage-class", "Standard");
    
            // Set the custom user metadata. The custom user metadata must be the same as the information used when the URL was generated.
            Map<String, String> userMetadata = new HashMap<>();
            userMetadata.put("key1", "value1");
            userMetadata.put("key2", "value2");
    
            new UploadTask().execute(signedUrl, pathName, headers, userMetadata);
        }
    
        private class UploadTask extends AsyncTask<Object, Void, Integer> {
            @Override
            protected Integer doInBackground(Object... params) {
                String signedUrl = (String) params[0];
                String pathName = (String) params[1];
                Map<String, String> headers = (Map<String, String>) params[2];
                Map<String, String> userMetadata = (Map<String, String>) params[3];
    
                try {
                    URL url = new URL(signedUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("PUT");
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
    
                    // Set the request headers.
                    for (Entry<String, String> header : headers.entrySet()) {
                        connection.setRequestProperty(header.getKey(), header.getValue());
                    }
    
                    // Set the custom user metadata.
                    for (Entry<String, String> meta : userMetadata.entrySet()) {
                        connection.setRequestProperty("x-oss-meta-" + meta.getKey(), meta.getValue());
                    }
    
                    // Read the file.
                    File file = new File(pathName);
                    FileInputStream fileInputStream = new FileInputStream(file);
                    DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
    
                    byte[] buffer = new byte[1024];
                    int count;
                    while ((count = fileInputStream.read(buffer)) != -1) {
                        dos.write(buffer, 0, count);
                    }
    
                    fileInputStream.close();
                    dos.flush();
                    dos.close();
    
                    // Get the response.
                    int responseCode = connection.getResponseCode();
                    Log.d(TAG, "Status code returned for the upload: " + responseCode);
                    if (responseCode == 200) {
                        Log.d(TAG, "Upload successful using network library.");
                    } else {
                        Log.d(TAG, "Upload failed.");
                    }
    
                    InputStream is = connection.getInputStream();
                    byte[] responseBuffer = new byte[1024];
                    StringBuilder responseStringBuilder = new StringBuilder();
                    while ((count = is.read(responseBuffer)) != -1) {
                        responseStringBuilder.append(new String(responseBuffer, 0, count));
                    }
                    Log.d(TAG, responseStringBuilder.toString());
    
                    return responseCode;
                } catch (IOException e) {
                    e.printStackTrace();
                    return -1;
                }
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);
                if (result == 200) {
                    Toast.makeText(SignUrlUploadActivity.this, "Upload successful.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(SignUrlUploadActivity.this, "Upload failed.", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    
  • Utiliser une URL pré-signée pour effectuer un chargement en plusieurs parties

    Pour réaliser un chargement en plusieurs parties à l'aide d'une URL pré-signée, vous devez configurer la taille des parties et générer une URL pré-signée pour chacune d'elles. L'exemple de code suivant illustre cette procédure :

    import argparse
    import os
    import requests
    import alibabacloud_oss_v2 as oss
    
    # Create a command-line argument parser and describe the script's purpose: to generate a presigned multipart upload request sample.
    parser = argparse.ArgumentParser(description="presign multipart upload sample")
    
    # Add the --region command-line argument, which indicates the region where the bucket is located. This is a required parameter.
    parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
    # Add the --bucket command-line argument, which indicates the name of the bucket to which you want to upload the object. This is a required parameter.
    parser.add_argument('--bucket', help='The name of the bucket.', required=True)
    # Add the --endpoint command-line argument, which indicates the domain name that other services can use to access OSS. This is an optional parameter.
    parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
    # Add the --key command-line argument, which indicates the key (file name) of the object in OSS. This is a required parameter.
    parser.add_argument('--key', help='The name of the object.', required=True)
    # Add the --file_path command-line argument, which indicates the path of the file to upload. This is a required parameter.
    parser.add_argument('--file_path', help='The path of Upload file.', required=True)
    
    def main():
        # Parse the command-line arguments to get the user-provided values.
        args = parser.parse_args()
    
        # Load the authentication information required to access OSS from environment variables for identity verification.
        credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
    
        # Create a configuration object using the SDK's default settings and set the authentication provider.
        cfg = oss.config.load_default()
        cfg.credentials_provider = credentials_provider
    
        # Set the region property of the configuration object based on the command-line arguments.
        cfg.region = args.region
    
        # If a custom endpoint is provided, update the endpoint property in the configuration object.
        if args.endpoint is not None:
            cfg.endpoint = args.endpoint
    
        # Initialize the OSS client with the above configuration to interact with OSS.
        client = oss.Client(cfg)
    
        # Part size is 1 MB.
        part_size = 1024 * 1024
        # Get the file size.
        data_size = os.path.getsize(args.file_path)
        part_number = 1
        upload_parts = []
    
        # Initialize the multipart upload request.
        init_pre_result = client.presign(oss.InitiateMultipartUploadRequest(
            bucket=args.bucket,
            key=args.key,
        ))
    
        # Initiate the request.
        with requests.post(init_pre_result.url, headers=init_pre_result.signed_headers) as resp:
            obj = oss.InitiateMultipartUploadResult()
            oss.serde.deserialize_xml(xml_data=resp.content, obj=obj)
    
            # Open the file for reading.
            with open(args.file_path, 'rb') as f:
                for start in range(0, data_size, part_size):
                    n = part_size
                    if start + n > data_size:
                        n = data_size - start
                    reader = oss.io_utils.SectionReader(oss.io_utils.ReadAtReader(f), start, n)
    
                    # Generate a presigned request to upload a part.
                    up_pre_result = client.presign(oss.UploadPartRequest(
                        bucket=args.bucket,
                        key=args.key,
                        upload_id=obj.upload_id,
                        part_number=part_number,
                    ))
    
                    # Upload the part data.
                    with requests.put(up_pre_result.url, headers=up_pre_result.signed_headers, data=reader) as up_result:
                        print(f'status code: {up_result.status_code},'
                              f' request id: {up_result.headers.get("x-oss-request-id")},'
                              f' part number: {part_number},'
                              f' hash crc64: {up_result.headers.get("x-oss-hash-crc64ecma")},'
                              f' content md5: {up_result.headers.get("Content-MD5")},'
                              f' etag: {up_result.headers.get("ETag")},'
                              f' server time: {up_result.headers.get("x-oss-server-time")}'
                        )
    
                        # Record the ETag and number of each part.
                        upload_parts.append(oss.UploadPart(part_number=part_number, etag=up_result.headers.get("ETag")))
                    part_number += 1
    
            # Sort the parts by part number.
            parts = sorted(upload_parts, key=lambda p: p.part_number)
    
            # Complete the multipart upload request.
            request = oss.CompleteMultipartUploadRequest(
                bucket=args.bucket,
                key=args.key,
                upload_id=obj.upload_id,
                complete_multipart_upload=oss.CompleteMultipartUpload(
                    parts=parts
                )
            )
    
            # Serialize the input for the complete request.
            op_input = oss.serde.serialize_input(request, oss.OperationInput(
                op_name='CompleteMultipartUpload',
                method='POST',
                bucket=request.bucket,
            ))
    
            # Generate a presigned request to complete the upload.
            complete_pre_result = client.presign(request)
    
            # Send the request to complete the upload.
            with requests.post(complete_pre_result.url, headers=complete_pre_result.signed_headers, data=op_input.body) as complete_resp:
                result = oss.CompleteMultipartUploadResult()
                oss.serde.deserialize_xml(xml_data=complete_resp.content, obj=result)
                print(f'status code: {complete_resp.status_code},'
                      f' request id: {complete_resp.headers.get("x-oss-request-id")},'
                      f' hash crc64: {complete_resp.headers.get("x-oss-hash-crc64ecma")},'
                      f' content md5: {complete_resp.headers.get("Content-MD5")},'
                      f' etag: {complete_resp.headers.get("ETag")},'
                      f' content length: {complete_resp.headers.get("content-length")},'
                      f' content type: {complete_resp.headers.get("Content-Type")},'
                      f' url: {result.location},'
                      f' encoding type: {result.encoding_type},'
                      f' server time: {complete_resp.headers.get("x-oss-server-time")}'
                )
    
            # Print the method, expiration time, and URL of the complete request.
            print(f'method: {complete_pre_result.method},'
                  f' expiration: {complete_pre_result.expiration.strftime("%Y-%m-%dT%H:%M:%S.000Z")},'
                  f' url: {complete_pre_result.url}'
            )
    
            # Print the signed header information of the complete request.
            for key, value in complete_pre_result.signed_headers.items():
                print(f'signed headers key: {key}, signed headers value: {value}')
    
    # When this script is executed directly, call the main function to start the processing logic.
    if __name__ == "__main__":
        main()  # Script entry point. The program flow starts here.

    Utiliser une URL pré-signée pour charger un fichier et configurer les paramètres de rappel de chargement

    1. Le propriétaire du fichier génère une URL pré-signée pour la méthode PUT avec des paramètres de rappel de chargement spécifiés.

      import argparse
      import base64
      import requests
      import alibabacloud_oss_v2 as oss
      
      from datetime import datetime, timedelta
      
      # Create a command-line argument parser and describe the script's purpose: to generate a presigned PUT request URL for an object (Presign Put Object).
      parser = argparse.ArgumentParser(description="presign put object sample")
      
      # Define command-line arguments, including the required region, bucket name, endpoint, and object key.
      parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
      parser.add_argument('--bucket', help='The name of the bucket.', required=True)
      parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
      parser.add_argument('--key', help='The name of the object.', required=True)
      
      def main():
          # Parse command-line arguments to get user-provided values.
          args = parser.parse_args()
      
          # Load access credential information from environment variables for identity verification.
          credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
      
          # Create a configuration object using the SDK's default settings and set the authentication provider.
          cfg = oss.config.load_default()
          cfg.credentials_provider = credentials_provider
      
          # Set the region property of the configuration object based on the command-line arguments.
          cfg.region = args.region
      
          # If a custom endpoint is provided, update the endpoint property in the configuration object.
          if args.endpoint is not None:
              cfg.endpoint = args.endpoint
      
          # Initialize the OSS client with the above configuration to interact with OSS.
          client = oss.Client(cfg)
      
          # Enter your custom webhook address.
          call_back_url = "http://www.example.com/callback"
          # Construct the callback parameter (callback): specify the webhook address and the callback request body, encoded in Base64.
          callback=base64.b64encode(str('{\"callbackUrl\":\"' + call_back_url + '\",\"callbackBody\":\"bucket=${bucket}&object=${object}&my_var_1=${x:var1}&my_var_2=${x:var2}\"}').encode()).decode()
          # Construct the custom variables (callback-var), encoded in Base64.
          callback_var=base64.b64encode('{\"x:var1\":\"value1\",\"x:var2\":\"value2\"}'.encode()).decode()
      
          # Send a request to generate a presigned PUT request for the specified object.
          pre_result = client.presign(oss.PutObjectRequest(
              bucket=args.bucket,  # Bucket name
              key=args.key,  # Object key
              callback=callback,
              callback_var=callback_var,
          ),expires=timedelta(seconds=3600)) # Set the expiration time in seconds. Here, it is set to 3600 seconds.
      
          # Print the method, expiration time, and URL of the presigned request to confirm the validity of the link.
          print(f'method: {pre_result.method},'
                f' expiration: {pre_result.expiration.strftime("%Y-%m-%dT%H:%M:%S.000Z")},'
                f' url: {pre_result.url}'
                )
      
          # Print the signed header information of the presigned request. This information will be included in the HTTP headers when the actual request is sent.
          for key, value in pre_result.signed_headers.items():
              print(f'signed headers key: {key}, signed headers value: {value}')
      
      # When this script is executed directly, call the main function to start the processing logic.
      if __name__ == "__main__":
          main()  # Script entry point. The program flow starts here.
    2. Un autre utilisateur charge un fichier en utilisant l'URL pré-signée pour la méthode PUT.

      curl

      curl -X PUT \
           -H "x-oss-callback: eyJjYWxsYmFja1VybCI6Imh0dHA6Ly93d3cuZXhhbXBsZS5jb20vY2FsbGJhY2siLCJjYWxsYmFja0JvZHkiOiJidWNrZXQ9JHtidWNrZXR9Jm9iamVjdD0ke29iamVjdH0mbXlfdmFyXzE9JHt4OnZhcjF9Jm15X3Zhcl8yPSR7eDp2YXIyfSJ9" \
           -H "x-oss-callback-var: eyJ4OnZhcjEiOiJ2YWx1ZTEiLCJ4OnZhcjIiOiJ2YWx1ZTIifQ==" \
           -T "C:\\Users\\demo.txt" \
           "https://exampleobject.oss-cn-hangzhou.aliyuncs.com/exampleobject.txt?x-oss-date=20241112T083238Z&x-oss-expires=3599&x-oss-signature-version=OSS4-HMAC-SHA256&x-oss-credential=LTAI****************%2F20241112%2Fcn-hangzhou%2Foss%2Faliyun_v4_request&x-oss-signature=ed5a******************************************************"

      Python

      import requests
      
      def upload_file(signed_url, file_path, headers=None):
          """
          Upload a file to OSS using a presigned URL.
      
          :param signed_url: The presigned URL.
          :param file_path: The full path of the file to upload.
          :param headers: Optional. Custom HTTP headers.
          :return: None
          """
          if not headers:
              headers = {}
      
          try:
              with open(file_path, 'rb') as file:
                  response = requests.put(signed_url, data=file, headers=headers)
                  print(f"Upload status code returned: {response.status_code}")
                  if response.status_code == 200:
                      print("Upload successful using the network library.")
                  else:
                      print("Upload failed.")
                  print(response.text)
          except Exception as e:
              print(f"An error occurred: {e}")
      
      if __name__ == "__main__":
          # Replace <signedUrl> with the authorized URL.
          signed_url = "<signedUrl>"
      
          # Enter the full path of the local file. If you do not specify a local path, the file is uploaded from the directory where the script is located by default.
          file_path = "C:\\Users\\demo.txt"
      
          headers = {
              "x-oss-callback": "eyJjYWxsYmFja1VybCI6Imh0dHA6Ly93d3cuZXhhbXBsZS5jb20vY2FsbGJhY2siLCJjYWxsYmFja0JvZHkiOiJidWNrZXQ9JHtidWNrZXR9Jm9iamVjdD0ke29iamVjdH0mbXlfdmFyXzE9JHt4OnZhcjF9Jm15X3Zhcl8yPSR7eDp2YXIyfSJ9",
              "x-oss-callback-var": "eyJ4OnZhcjEiOiJ2YWx1ZTEiLCJ4OnZhcjIiOiJ2YWx1ZTIifQ==",
          }
      
          upload_file(signed_url,  file_path, headers)

      Go

      package main
      
      import (
          "bytes"
          "fmt"
          "io"
          "net/http"
          "os"
      )
      
      func uploadFile(signedUrl string, filePath string, headers map[string]string) error {
          // Open the file.
          file, err := os.Open(filePath)
          if err != nil {
              return err
          }
          defer file.Close()
      
          // Read the file content.
          fileBytes, err := io.ReadAll(file)
          if err != nil {
              return err
          }
      
          // Create a request.
          req, err := http.NewRequest("PUT", signedUrl, bytes.NewBuffer(fileBytes))
          if err != nil {
              return err
          }
      
          // Set request headers.
          for key, value := range headers {
              req.Header.Add(key, value)
          }
      
          // Send the request.
          client := &http.Client{}
          resp, err := client.Do(req)
          if err != nil {
              return err
          }
          defer resp.Body.Close()
      
          // Process the response.
          fmt.Printf("Upload status code returned: %d\n", resp.StatusCode)
          if resp.StatusCode == 200 {
              fmt.Println("Upload successful using the network library.")
          } else {
              fmt.Println("Upload failed.")
          }
          body, _ := io.ReadAll(resp.Body)
          fmt.Println(string(body))
      
          return nil
      }
      
      func main() {
          // Replace <signedUrl> with the authorized URL.
          signedUrl := "<signedUrl>"
          // Enter the full path of the local file. If you do not specify a local path, the file is uploaded from the project's corresponding local path by default.
          filePath := "C:\\Users\\demo.txt"
      
          // Set request headers. The header information here must be consistent with the information used to generate the URL.
          headers := map[string]string{
              "x-oss-callback":     "eyJjYWxsYmFja0JvZHkiOiJidWNrZXQ9JHtidWNrZXR9XHUwMDI2b2JqZWN0PSR7b2JqZWN0fVx1MDAyNnNpemU9JHtzaXplfVx1MDAyNm15X3Zhcl8xPSR7eDpteV92YXIxfVx1MDAyNm15X3Zhcl8yPSR7eDpteV92YXIyfSIsImNhbGxiYWNrQm9keVR5cGUiOiJhcHBsaWNhdGlvbi94LXd3dy1mb3JtLXVybGVuY29kZWQiLCJjYWxsYmFja1VybCI6Imh0dHA6Ly9leGFtcGxlLmNvbToyMzQ1MCJ9",
              "x-oss-callback-var": "eyJ4Om15X3ZhcjEiOiJ0aGkgaXMgdmFyIDEiLCJ4Om15X3ZhcjIiOiJ0aGkgaXMgdmFyIDIifQ==",
          }
      
          err := uploadFile(signedUrl, filePath, headers)
          if err != nil {
              fmt.Printf("An error occurred: %v\n", err)
          }
      }

      Java

      import org.apache.http.HttpEntity;
      import org.apache.http.client.methods.CloseableHttpResponse;
      import org.apache.http.client.methods.HttpPut;
      import org.apache.http.entity.FileEntity;
      import org.apache.http.impl.client.CloseableHttpClient;
      import org.apache.http.impl.client.HttpClients;
      import java.io.*;
      import java.net.URL;
      import java.util.*;
      
      public class SignUrlUpload {
        public static void main(String[] args) throws IOException {
          CloseableHttpClient httpClient = null;
          CloseableHttpResponse response = null;
      
          // Replace <signedUrl> with the authorized URL.
          URL signedUrl = new URL("<signedUrl>");
      
          // Enter the full path of the local file. If you do not specify a local path, the file is uploaded from the project's corresponding local path by default.
          String pathName = "C:\\Users\\demo.txt";
      
          // Set request headers, including x-oss-callback and x-oss-callback-var.
          Map<String, String> headers = new HashMap<String, String>();
          headers.put("x-oss-callback", "eyJjYWxsYmFja1VybCI6Imh0dHA6Ly93d3cuZXhhbXBsZS5jb20vY2FsbGJhY2siLCJjYWxsYmFja0JvZHkiOiJidWNrZXQ9JHtidWNrZXR9Jm9iamVjdD0ke29iamVjdH0mbXlfdmFyXzE9JHt4OnZhcjF9Jm15X3Zhcl8yPSR7eDp2YXIyfSJ9");
          headers.put("x-oss-callback-var", "eyJ4OnZhcjEiOiJ2YWx1ZTEiLCJ4OnZhcjIiOiJ2YWx1ZTIifQ==");
      
          try {
            HttpPut put = new HttpPut(signedUrl.toString());
            System.out.println(put);
            HttpEntity entity = new FileEntity(new File(pathName));
            put.setEntity(entity);
            // If header parameters were set when generating the presigned URL, these parameters must also be sent to the server when using the presigned URL to upload the file. If the signature and the parameters sent to the server do not match, a signature error will occur.
            for(Map.Entry<String, String> header: headers.entrySet()){
              put.addHeader(header.getKey(),header.getValue());
            }
      
            httpClient = HttpClients.createDefault();
      
            response = httpClient.execute(put);
      
            System.out.println("Upload status code returned:"+response.getStatusLine().getStatusCode());
            if(response.getStatusLine().getStatusCode() == 200){
              System.out.println("Upload successful using the network library.");
            }
            System.out.println(response.toString());
          } catch (Exception e){
            e.printStackTrace();
          } finally {
            if (response != null) {
              response.close();
            }
            if (httpClient != null) {
              httpClient.close();
            }
          }
        }
      }       

      PHP

      <?php
      
      function uploadFile($signedUrl, $filePath, $headers = []) {
          // Check if the file exists.
          if (!file_exists($filePath)) {
              echo "File does not exist: $filePath\n";
              return;
          }
      
          // Initialize cURL session.
          $ch = curl_init();
      
          // Set cURL options.
          curl_setopt($ch, CURLOPT_URL, $signedUrl);
          curl_setopt($ch, CURLOPT_PUT, true);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb'));
          curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
          curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function($key, $value) {
              return "$key: $value";
          }, array_keys($headers), $headers));
      
          // Execute cURL request.
          $response = curl_exec($ch);
      
          // Get HTTP status code.
          $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      
          // Close cURL session.
          curl_close($ch);
      
          // Output the result.
          echo "Upload status code returned: $httpCode\n";
          if ($httpCode == 200) {
              echo "Upload successful using the network library.\n";
          } else {
              echo "Upload failed.\n";
          }
          echo $response . "\n";
      }
      
      // Replace <signedUrl> with the authorized URL.
      $signedUrl = "<signedUrl>";
      
      // Enter the full path of the local file. If you do not specify a local path, the file is uploaded from the directory where the script is located by default.
      $filePath = "C:\\Users\\demo.txt";
      
      $headers = [
          "x-oss-callback" => "eyJjYWxsYmFja1VybCI6Imh0dHA6Ly93d3cuZXhhbXBsZS5jb20vY2FsbGJhY2siLCJjYWxsYmFja0JvZHkiOiJidWNrZXQ9JHtidWNrZXR9Jm9iamVjdD0ke29iamVjdH0mbXlfdmFyXzE9JHt4OnZhcjF9Jm15X3Zhcl8yPSR7eDp2YXIyfSJ9",
          "x-oss-callback-var" => "eyJ4OnZhcjEiOiJ2YWx1ZTEiLCJ4OnZhcjIiOiJ2YWx1ZTIifQ==",
      ];
      
      uploadFile($signedUrl, $filePath, $headers);
      
      ?>

    Par défaut, les objects stockés dans un compartiment Object Storage Service (OSS) sont privés. Seul le propriétaire de l'object dispose des autorisations nécessaires pour effectuer des chargements. Vous pouvez utiliser le SDK OSS pour Python afin de générer une URL pré-signée permettant à d'autres utilisateurs de charger un object. Lors de la génération de cette URL, vous avez la possibilité de définir une durée d'expiration. Tant qu'elle n'a pas expiré, l'URL peut être utilisée plusieurs fois. Attention toutefois : si plusieurs opérations de chargement sont effectuées, l'object risque d'être écrasé. Une fois l'URL arrivée à expiration, elle ne peut plus servir aux chargements. Il vous faudra alors générer une nouvelle URL pré-signée.

    Instructions

    Remarques d'utilisation

    • Le code exemple de cette rubrique utilise la région Chine (Hangzhou) (cn-hangzhou) à titre d'exemple. Un endpoint public est utilisé par défaut. Si vous accédez à OSS depuis un autre service Alibaba Cloud dans la même région, vous devez utiliser un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

    • Aucune autorisation n'est requise pour générer une URL présignée. Toutefois, un tiers ne peut télécharger un fichier avec succès via l'URL présignée que si vous disposez de l'autorisation oss:PutObject. Pour plus d'informations, consultez Accorder une politique d'accès personnalisée à un utilisateur RAM.

    • Le code exemple de cette rubrique utilise des URL présignées V4, dont la durée de validité maximale est de 7 jours. Pour plus d'informations, consultez Signature V4 (recommandée).

    Processus

    Pour télécharger un fichier à l'aide d'une URL signée et de la méthode PUT, suivez la procédure ci-dessous :

    image

    Définition de la méthode

    Vous pouvez générer une URL présignée qui accorde des autorisations d'accès temporaires aux objects d'un bucket et qui peut être utilisée plusieurs fois avant son expiration.

    La méthode presign est définie comme suit :

    presign(request: PutObjectRequest, **kwargs) → PresignResult

    Paramètres de requête

    Nom du paramètre

    Type

    Description

    request

    PutObjectRequest

    Nom de la méthode pour laquelle générer l'URL présignée. Pour plus d'informations, consultez Client.presign.

    expires

    datetime.timedelta

    (Facultatif) Durée de vie (TTL) de l'URL présignée, à compter de l'heure actuelle. Par exemple, pour définir une TTL de 30 minutes, utilisez 30 * time.Minute. Si ce paramètre n'est pas spécifié, la valeur par défaut est de 15 minutes.

    expiration

    datetime.datetime

    (Facultatif) Date et heure spécifiques d'expiration de l'URL présignée.

    Important

    Pour les signatures V4, la durée de validité maximale est de 7 jours. Si vous définissez à la fois Expiration et Expires, le paramètre Expiration est prioritaire.

    Valeurs de retour

    Type

    Description

    PresignResult

    Résultat incluant l'URL présignée, la méthode HTTP, l'heure d'expiration et les en-têtes de requête signés. Pour plus d'informations, consultez PresignResult.

    Les valeurs de retour de PresignResult sont répertoriées ci-dessous :

    Nom du paramètre

    Type

    Description

    method

    str

    Méthode HTTP correspondant à l'interface. Par exemple, pour l'interface PutObject, la valeur PUT est renvoyée.

    url

    str

    L'URL présignée.

    expiration

    datetime

    L'heure d'expiration de l'URL présignée.

    signed_headers

    MutableMapping

    En-têtes de requête signés. Par exemple, si vous définissez content_type, les informations relatives à content_type sont renvoyées.

    Pour la définition complète de la méthode presign, consultez presign.

    Exemple de code

    1. Le propriétaire du fichier génère une URL présignée pour la méthode PUT.

      Important

      Lorsque vous générez une URL présignée pour la méthode PUT, si vous spécifiez des en-têtes de requête, vous devez inclure les mêmes en-têtes dans la requête PUT utilisant l'URL présignée. Dans le cas contraire, la vérification de la signature échoue.

      import argparse
      import requests
      import alibabacloud_oss_v2 as oss
      
      from datetime import datetime, timedelta
      
      # Create a command-line argument parser and describe the script's purpose: to generate a presigned PUT request URL for an object (Presign Put Object).
      parser = argparse.ArgumentParser(description="presign put object sample")
      
      # Define command-line arguments, including the required region, bucket name, endpoint, and object key.
      parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
      parser.add_argument('--bucket', help='The name of the bucket.', required=True)
      parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
      parser.add_argument('--key', help='The name of the object.', required=True)
      
      def main():
          # Parse command-line arguments to get user-provided values.
          args = parser.parse_args()
      
          # Load access credential information from environment variables for identity verification.
          credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
      
          # Create a configuration object using the SDK's default settings and set the authentication provider.
          cfg = oss.config.load_default()
          cfg.credentials_provider = credentials_provider
      
          # Set the region property of the configuration object based on the command-line arguments.
          cfg.region = args.region
      
          # If a custom endpoint is provided, update the endpoint property in the configuration object.
          if args.endpoint is not None:
              cfg.endpoint = args.endpoint
      
          # Initialize the OSS client with the above configuration to interact with OSS.
          client = oss.Client(cfg)
      
          # Send a request to generate a presigned PUT request for the specified object.
          pre_result = client.presign(oss.PutObjectRequest(
              bucket=args.bucket,  # Bucket name
              key=args.key,  # Object key
          ),expires=timedelta(seconds=3600)) # Set the expiration time in seconds. Here, it is set to 3600 seconds.
      
          # Print the method, expiration time, and URL of the presigned request to confirm the validity of the link.
          print(f'method: {pre_result.method},'
                f' expiration: {pre_result.expiration.strftime("%Y-%m-%dT%H:%M:%S.000Z")},'
                f' url: {pre_result.url}'
                )
      
          # Print the signed header information of the presigned request. This information will be included in the HTTP headers when the actual request is sent.
          for key, value in pre_result.signed_headers.items():
              print(f'signed headers key: {key}, signed headers value: {value}')
      
      # When this script is executed directly, call the main function to start the processing logic.
      if __name__ == "__main__":
          main()  # Script entry point. The program flow starts here.
    2. Un autre utilisateur télécharge un fichier à l'aide de l'URL présignée pour la méthode PUT.

      curl

      curl -X PUT -T /path/to/local/file "https://exampleobject.oss-cn-hangzhou.aliyuncs.com/exampleobject.txt?x-oss-date=20241112T083238Z&x-oss-expires=3599&x-oss-signature-version=OSS4-HMAC-SHA256&x-oss-credential=LTAI****************%2F20241112%2Fcn-hangzhou%2Foss%2Faliyun_v4_request&x-oss-signature=ed5a******************************************************"

      Java

      import org.apache.http.HttpEntity;
      import org.apache.http.client.methods.CloseableHttpResponse;
      import org.apache.http.client.methods.HttpPut;
      import org.apache.http.entity.FileEntity;
      import org.apache.http.impl.client.CloseableHttpClient;
      import org.apache.http.impl.client.HttpClients;
      import java.io.*;
      import java.net.URL;
      import java.util.*;
      
      public class SignUrlUpload {
          public static void main(String[] args) throws Throwable {
              CloseableHttpClient httpClient = null;
              CloseableHttpResponse response = null;
      
              // Replace <signedUrl> with the authorized URL.
              URL signedUrl = new URL("<signedUrl>");
      
              // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
              String pathName = "C:\\Users\\demo.txt";
      
              try {
                  HttpPut put = new HttpPut(signedUrl.toString());
                  System.out.println(put);
                  HttpEntity entity = new FileEntity(new File(pathName));
                  put.setEntity(entity);
                  httpClient = HttpClients.createDefault();
                  response = httpClient.execute(put);
      
                  System.out.println("Status code returned for the upload:"+response.getStatusLine().getStatusCode());
                  if(response.getStatusLine().getStatusCode() == 200){
                      System.out.println("Upload successful using network library.");
                  }
                  System.out.println(response.toString());
              } catch (Exception e){
                  e.printStackTrace();
              } finally {
                   if (response != null) {
                       response.close();
                   }
                   if (httpClient != null) {
                       httpClient.close();
                   }
               }
          }
      }       

      Go

      package main
      
      import (
      	"fmt"
      	"io"
      	"net/http"
      	"os"
      )
      
      func uploadFile(signedUrl, filePath string) error {
      	// Open the file.
      	file, err := os.Open(filePath)
      	if err != nil {
      		return fmt.Errorf("Unable to open file: %w", err)
      	}
      	defer file.Close()
      
      	// Create a new HTTP client.
      	client := &http.Client{}
      
      	// Create a PUT request.
      	req, err := http.NewRequest("PUT", signedUrl, file)
      	if err != nil {
      		return fmt.Errorf("Failed to create request: %w", err)
      	}
      
      	// Send the request.
      	resp, err := client.Do(req)
      	if err != nil {
      		return fmt.Errorf("Failed to send request: %w", err)
      	}
      	defer resp.Body.Close()
      
      	// Read the response.
      	body, err := io.ReadAll(resp.Body)
      	if err != nil {
      		return fmt.Errorf("Failed to read response: %w", err)
      	}
      
      	fmt.Printf("Status code returned for the upload: %d\n", resp.StatusCode)
      	if resp.StatusCode == 200 {
      		fmt.Println("Upload successful using network library.")
      	}
      	fmt.Println(string(body))
      
      	return nil
      }
      
      func main() {
      	// Replace <signedUrl> with the authorized URL.
      	signedUrl := "<signedUrl>"
      
      	// Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
      	filePath := "C:\\Users\\demo.txt"
      
      	err := uploadFile(signedUrl, filePath)
      	if err != nil {
      		fmt.Println("An error occurred:", err)
      	}
      }
      

      python

      import requests
      
      def upload_file(signed_url, file_path):
          try:
              # Open the file.
              with open(file_path, 'rb') as file:
                  # Send a PUT request to upload the file.
                  response = requests.put(signed_url, data=file)
           
              print(f"Status code returned for the upload: {response.status_code}")
              if response.status_code == 200:
                  print("Upload successful using network library.")
              print(response.text)
       
          except Exception as e:
              print(f"An error occurred: {e}")
      
      if __name__ == "__main__":
          # Replace <signedUrl> with the authorized URL.
          signed_url = "<signedUrl>"
          
          # Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
          file_path = "C:\\Users\\demo.txt"
      
          upload_file(signed_url, file_path)
      

      Node.js

      Important

      Si vous rencontrez une erreur de non-correspondance de signature 403 lors du téléchargement d'un fichier avec Node.js, cela est généralement dû au fait que l'en-tête de requête Content-Type est explicitement défini dans la requête de téléchargement, mais qu'il n'a pas été inclus lors de la génération de l'URL présignée. Cette incompatibilité entraîne un échec de la vérification de la signature. Pour résoudre ce problème, ne définissez pas l'en-tête de requête Content-Type dans la requête de téléchargement ou spécifiez l'en-tête de requête Content-Type lors de la génération de l'URL présignée.

      const fs = require('fs');
      const axios = require('axios');
      
      async function uploadFile(signedUrl, filePath) {
          try {
              // Create a read stream.
              const fileStream = fs.createReadStream(filePath);
      
              // Send a PUT request to upload the file.
              const response = await axios.put(signedUrl, fileStream);
      
              console.log(`Status code returned for the upload: ${response.status}`);
              if (response.status === 200) {
                  console.log('Upload successful using network library.');
              }
              console.log(response.data);
          } catch (error) {
              console.error(`An error occurred: ${error.message}`);
          }
      }
      
      // Main function.
      (async () => {
          // Replace <signedUrl> with the authorized URL.
          const signedUrl = '<signedUrl>';
          
          // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
          const filePath = 'C:\\Users\\demo.txt';
      
          await uploadFile(signedUrl, filePath);
      })();

      browser.js

      Important

      Si vous rencontrez une erreur de non-correspondance de signature 403 lors du téléchargement d'un fichier avec Browser.js, cela est généralement dû au fait que le navigateur ajoute automatiquement l'en-tête de requête Content-Type et que cet en-tête n'a pas été inclus lors de la génération de l'URL présignée. Cette incompatibilité entraîne un échec de la vérification de la signature. Pour résoudre ce problème, vous devez spécifier l'en-tête de requête Content-Type lors de la génération de l'URL présignée.

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>File Upload Example</title>
      </head>
      <body>
          <h1>File Upload Example</h1>
      
          <!-- Select a file -->
          <input type="file" id="fileInput" />
          <button id="uploadButton">Upload File</button>
      
          <script>
              // Replace this with the presigned URL generated in Step 1.
              const signedUrl = "<signedUrl>"; 
      
              document.getElementById('uploadButton').addEventListener('click', async () => {
                  const fileInput = document.getElementById('fileInput');
                  const file = fileInput.files[0];
      
                  if (!file) {
                      alert('Please select a file to upload.');
                      return;
                  }
      
                  try {
                      await upload(file, signedUrl);
                      alert('File uploaded successfully!');
                  } catch (error) {
                      console.error('Error during upload:', error);
                      alert('Upload failed: ' + error.message);
                  }
              });
      
              /**
               * Upload the file to OSS.
               * @param {File} file - The file to upload.
               * @param {string} presignedUrl - The presigned URL.
               */
              const upload = async (file, presignedUrl) => {
                  const response = await fetch(presignedUrl, {
                      method: 'PUT',
                      body: file,  // Upload the entire file directly.
                  });
      
                  if (!response.ok) {
                      throw new Error(`Upload failed, status: ${response.status}`);
                  }
      
                  console.log('File uploaded successfully');
              };
          </script>
      </body>
      </html>

      C#

      using System.Net.Http.Headers;
      
      // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
      var filePath = "C:\\Users\\demo.txt";
      // Replace <signedUrl> with the authorized URL.
      var presignedUrl = "<signedUrl>";
      
      // Create an HTTP client and open the local file stream.
      using var httpClient = new HttpClient(); 
      using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
      using var content = new StreamContent(fileStream);
                  
      // Create a PUT request.
      var request = new HttpRequestMessage(HttpMethod.Put, presignedUrl);
      request.Content = content;
      
      // Send the request.
      var response = await httpClient.SendAsync(request);
      
      // Process the response.
      if (response.IsSuccessStatusCode)
      {
          Console.WriteLine($"Upload successful! Status code: {response.StatusCode}");
          Console.WriteLine("Response headers:");
          foreach (var header in response.Headers)
          {
              Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
          }
      }
      else
      {
          string responseContent = await response.Content.ReadAsStringAsync();
          Console.WriteLine($"Upload failed! Status code: {response.StatusCode}");
          Console.WriteLine("Response content: " + responseContent);
      }

      C++

      #include <iostream>
      #include <fstream>
      #include <curl/curl.h>
      
      void uploadFile(const std::string& signedUrl, const std::string& filePath) {
          CURL *curl;
          CURLcode res;
      
          curl_global_init(CURL_GLOBAL_DEFAULT);
          curl = curl_easy_init();
      
          if (curl) {
              // Set the URL.
              curl_easy_setopt(curl, CURLOPT_URL, signedUrl.c_str());
      
              // Set the request method to PUT.
              curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
      
              // Open the file.
              FILE *file = fopen(filePath.c_str(), "rb");
              if (!file) {
                  std::cerr << "Unable to open file: " << filePath << std::endl;
                  return;
              }
      
              // Get the file size.
              fseek(file, 0, SEEK_END);
              long fileSize = ftell(file);
              fseek(file, 0, SEEK_SET);
      
              // Set the file size.
              curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
      
              // Set the input file handle.
              curl_easy_setopt(curl, CURLOPT_READDATA, file);
      
              // Execute the request.
              res = curl_easy_perform(curl);
      
              if (res != CURLE_OK) {
                  std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
              } else {
                  long httpCode = 0;
                  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
                  std::cout << "Status code returned for the upload: " << httpCode << std::endl;
      
                  if (httpCode == 200) {
                      std::cout << "Upload successful using network library." << std::endl;
                  }
              }
      
              // Close the file.
              fclose(file);
      
              // Clean up.
              curl_easy_cleanup(curl);
          }
      
          curl_global_cleanup();
      }
      
      int main() {
          // Replace <signedUrl> with the authorized URL.
          std::string signedUrl = "<signedUrl>";
      
          // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
          std::string filePath = "C:\\Users\\demo.txt";
      
          uploadFile(signedUrl, filePath);
      
          return 0;
      }
      

      Android

      package com.example.signurlupload;
      
      import android.os.AsyncTask;
      import android.util.Log;
      
      import java.io.DataOutputStream;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.net.HttpURLConnection;
      import java.net.URL;
      
      public class SignUrlUploadActivity {
      
          private static final String TAG = "SignUrlUploadActivity";
      
          public void uploadFile(String signedUrl, String filePath) {
              new UploadTask().execute(signedUrl, filePath);
          }
      
          private class UploadTask extends AsyncTask<String, Void, String> {
      
              @Override
              protected String doInBackground(String... params) {
                  String signedUrl = params[0];
                  String filePath = params[1];
      
                  HttpURLConnection connection = null;
                  DataOutputStream dos = null;
                  FileInputStream fis = null;
      
                  try {
                      URL url = new URL(signedUrl);
                      connection = (HttpURLConnection) url.openConnection();
                      connection.setRequestMethod("PUT");
                      connection.setDoOutput(true);
                      connection.setRequestProperty("Content-Type", "application/octet-stream");
      
                      fis = new FileInputStream(filePath);
                      dos = new DataOutputStream(connection.getOutputStream());
      
                      byte[] buffer = new byte[1024];
                      int length;
      
                      while ((length = fis.read(buffer)) != -1) {
                          dos.write(buffer, 0, length);
                      }
      
                      dos.flush();
                      dos.close();
                      fis.close();
      
                      int responseCode = connection.getResponseCode();
                      Log.d(TAG, "Status code returned for the upload: " + responseCode);
      
                      if (responseCode == 200) {
                          Log.d(TAG, "Upload successful using network library.");
                      }
      
                      return "Upload complete. Status code: " + responseCode;
      
                  } catch (IOException e) {
                      e.printStackTrace();
                      return "Upload failed: " + e.getMessage();
                  } finally {
                      if (connection != null) {
                          connection.disconnect();
                      }
                  }
              }
      
              @Override
              protected void onPostExecute(String result) {
                  Log.d(TAG, result);
              }
          }
      
          public static void main(String[] args) {
              SignUrlUploadActivity activity = new SignUrlUploadActivity();
              // Replace <signedUrl> with the authorized URL.
              String signedUrl = "<signedUrl>";
              // Specify the full path of the local file. If you do not specify a local path, the file is uploaded from the project path of the sample program by default.
              String filePath = "C:\\Users\\demo.txt";
              activity.uploadFile(signedUrl, filePath);
          }
      }
      

    FAQ

    Si j'utilise une signature temporaire pour télécharger un fichier et que la signature expire pendant le téléchargement, le téléchargement échouera-t-il ?

    Le téléchargement aboutira.

    Une URL présignée est utilisée pour les téléchargements et reste valide pendant la durée minimale entre la période de validité du jeton et sa propre période de validité.

    Si je ne définis pas d'en-têtes de requête ni de métadonnées personnalisées lors de la génération de l'URL, dois-je les configurer lors de l'utilisation de l'URL pour un téléchargement ?

    Aucune configuration n'est requise.

    Les en-têtes de requête et les métadonnées personnalisées sont des paramètres facultatifs. Si vous ne les définissez pas lors de la génération de l'URL, vous n'avez pas besoin de les inclure dans la requête de téléchargement.

    Références

    • Pour le code exemple complet relatif au téléchargement d'un fichier à l'aide d'une URL présignée, consultez presigner_put_object.py.

    • Pour le code exemple complet relatif à l'exécution d'un téléchargement multipartie à l'aide d'une URL présignée, consultez presigner_complete_multipart_upload.py.