Tüm Ürünler
Search
Belge Merkezi

ApsaraVideo VOD:Upload files using the Python SDK

Son Güncelleme:Oct 05, 2026

Bu konu, çeşitli medya dosyalarını ApsaraVideo VOD'a yüklemek için Python sunucu tarafı yükleme SDK'sının nasıl kullanılacağına ilişkin ayrıntılı örnekler sunar.

Genel bakış

Python yükleme SDK'sı, ApsaraVideo VOD sunucu tarafı SDK'sının genel iş akışını izler. Daha fazla bilgi için bkz. Yükleme işlemi. Python yükleme SDK'sını kullanmanın temel adımları aşağıda verilmiştir:

  1. Ön koşulları tamamlayın. Daha fazla bilgi için bkz. Ön koşullar.

  2. Python yükleme SDK'sını entegre edin. Daha fazla bilgi için bkz. Python yükleme SDK'sını entegre etme.

  3. Yükleme mantığını uygulayın. Bu adım, yükleme bilgilerinin yapılandırılmasını içerir.

  4. Python yükleme SDK'sı için varsayılan uç nokta cn-shanghai (Şanghay) değeridir. Medya dosyalarını diğer bölgelere yüklemek için AliyunVodUploader.py içindeki ApiRegion parametresini kullanarak bölgeyi belirtebilirsiniz. ApsaraVideo VOD'un desteklediği uç noktalar hakkında daha fazla bilgi için bkz. Uç noktalar.

    Aşağıdaki örnekte yükleme bölgesinin cn-beijing (Pekin) olarak nasıl ayarlanacağı gösterilmektedir:

    self.setApiRegion('cn-beijing')

Ön koşullar

  • ApsaraVideo VOD etkinleştirilmiştir. Daha fazla bilgi için bkz. ApsaraVideo VOD'u etkinleştirme.

  • Belirtilen bölgedeki depolama yolu ve geri arama ayarları dahil olmak üzere yükleme için gereken sistem ayarları yapılandırılmıştır. Daha fazla bilgi için bkz. Depolama paketlerini yönetme ve Geri aramaları yapılandırma.

  • Bir RAM kullanıcısı oluşturulmuş ve ApsaraVideo VOD'a erişmek için bu kullanıcı kullanılmaktadır. Alibaba Cloud hesabınızın AccessKey çiftinin sızması nedeniyle oluşabilecek güvenlik risklerini önlemek için bir RAM kullanıcısı oluşturmanızı ve bu kullanıcıya ApsaraVideo VOD'a erişmek için gereken izinleri vermenizi öneririz. Ardından, ApsaraVideo VOD'a erişmek için RAM kullanıcısının AccessKey çiftini kullanabilirsiniz. Daha fazla bilgi için bkz. RAM kullanıcısı oluşturma ve izin verme.

  • ALIBABA_CLOUD_ACCESS_KEY_ID ve ALIBABA_CLOUD_ACCESS_KEY_SECRET ortam değişkenlerini yapılandırın. Daha fazla bilgi için bkz. Linux, macOS ve Windows'ta ortam değişkenlerini yapılandırma.

    Önemli
    • Bir Alibaba Cloud hesabı AccessKey çifti tüm API işlemleri üzerinde yetkiye sahiptir. API işlemlerini çağırmak veya rutin bakım ve işletim işlemlerini yürütmek için bir RAM kullanıcısının AccessKey çiftini kullanın.

    • AccessKey ID'nizi ve AccessKey gizli anahtarınızı proje koduna sabit kodlamayın. Sızdırılması durumunda hesabınızdaki tüm kaynaklar tehlikeye girer.

  • İsteğe bağlı. ApsaraVideo VOD'a Security Token Service (STS) kullanarak erişmek istiyorsanız RAM kullanıcısı için bir rol oluşturun ve bu role ApsaraVideo VOD'a erişmek için gereken izinleri verin. Daha fazla bilgi için bkz. STS belirteci alma.

Python yükleme SDK'sını entegre etme

Not
  • Yükleme SDK'sı dizinlerinin ayrıntılı açıklaması için bkz. Dizin yapısı.

  • Bu konuda işlemler normal bir kullanıcı kullanılarak açıklanmıştır.

  1. Bağımlılık paketlerini yüklemek için aşağıdaki komutları çalıştırın.

    sudo pip install aliyun-python-sdk-core

    sudo pip install aliyun-python-sdk-vod

    sudo pip install oss2

    Not

    Python 3 kullanıyorsanız sudo pip install aliyun-python-sdk-core komutunu sudo pip install aliyun-python-sdk-core-v3 olarak değiştirin. Birden fazla Python sürümü yüklüyse pip3 komutunu kullanabilirsiniz. Entegrasyon sorunlarıyla karşılaşırsanız sorunları gidermek için SSS bölümüne bakın.

  2. Python yükleme SDK'sını ve örnek kodu indirin.

  3. VodUploadSDK-Python_1.3.*.zip dosyasını ayıklayın ve VodUploadSDK-Python_1.3.* dizinindeki voduploadsdk klasörünü yerel proje dizininize (önerilen) veya site-packages dizinine kopyalayın.

Python yükleme SDK'sını güncelleme

Mevcut SDK'nızda yeni API'lerin veya mevcut API'lerin yeni özelliklerinin bulunmadığını fark ederseniz SDK'yı en son sürüme güncelleyin.

  1. Bağımlılık paketlerini güncellemek için aşağıdaki komutları çalıştırın.

    sudo pip install --upgrade aliyun-python-sdk-vod

    sudo pip install --upgrade oss2

  2. En son Python yükleme SDK'sını indirin ve yerel SDK dosyalarının üzerine yazın. Daha fazla bilgi için bkz. SDK indirme.

    Not

    Mevcut SDK sürüm numarasını ve yayın tarihini görüntülemek için voduploadsdk dizinindeki ChangeLog.txt dosyasını açabilirsiniz.

Python yükleme SDK'sı dizin yapısı

voduploadsdk dizini

  • AliyunVodUtils.py


    Dizin


    Açıklama


    AliyunVodLog


    Yükleme SDK'sının günlük sınıfı. logging modülü temel alınarak uygulanmıştır.


    AliyunVodUtils


    Yükleme SDK'sının yardımcı program sınıfı.


    AliyunVodException


    Yükleme SDK'sının özel durum sınıfı. Birleşik özel durum işlemeyi sağlar. Bu özel durumu harici olarak yakalayabilirsiniz.

  • UploadVideoRequest.py


    Dizin


    Açıklama


    UploadVideoRequest


    Video yükleme isteği sınıfı. Alanlar hakkında daha fazla bilgi için bkz. CreateUploadVideo.

  • UploadImageRequest.py


    Dizin


    Açıklama


    UploadImageRequest


    Görüntü yükleme isteği sınıfı. Alanlar hakkında daha fazla bilgi için bkz. CreateUploadImage.

  • UploadAttachedMediaRequest.py


    Dizin


    Açıklama


    UploadAttachedMediaRequest


    Yardımcı medya varlıklarını yükleme isteği sınıfı. Alanlar hakkında daha fazla bilgi için bkz. CreateUploadAttachedMedia.

  • AliyunVodUploader.py


    Dizin


    Açıklama


    uploadLocalVideo


    Yerel videoları yüklemek için kullanılan API.


    uploadWebVideo


    Web videolarını yüklemek için kullanılan API.


    uploadLocalM3u8


    Yerel M3U8 videolarını yükler.


    uploadWebM3u8


    Web M3U8 videolarını yükler.


    uploadImage


    Yerel veya web görüntü dosyalarını yükler.


    uploadAttachedMedia


    Yerel veya web yardımcı medya varlığı dosyalarını yükler.


    parseWebM3u8


    Bir web M3U8 dosyasının parça bilgilerini ayrıştırır.


    parseLocalM3u8


    Yerel bir M3U8 dosyasının parça bilgilerini ayrıştırır.


    setApiRegion


    VOD için uç noktayı ayarlar. Varsayılan değer cn-shanghai (Şanghay) şeklindedir. ap-southeast-1 (Singapur) gibi Çin dışındaki bölgeler de desteklenir. Daha fazla bilgi için bkz. VOD bölge kimlikleri.


    setMultipartUpload


    Çok parçalı yükleme için eşik değerini ve parça boyutunu ayarlar.


    uploadProgressCallback


    Yükleme ilerlemesi için geri arama işlevi. Bu işlevi yeniden yazabilirsiniz.


    setEnableCrc


    Yükleme sırasında döngüsel yedeklilik denetiminin (CRC) etkinleştirilip etkinleştirilmeyeceğini belirtir. Varsayılan olarak etkindir.

  • ChangeLog.txt: Yayın geçmişini içerir. İlk satır mevcut SDK sürüm numarasını ve yayın tarihini gösterir.

samples dizini

  • uploadVideo.py: Video yüklemeye yönelik örnek kod.

  • uploadImage.py: Görüntü yüklemeye yönelik örnek kod.

  • uploadAttachedMedia.py: Yardımcı medya varlığı dosyalarını yüklemeye yönelik örnek kod.

Senaryo 1: Ses ve video dosyalarını yükleme

Normal dosyaları yükleme

Aşağıdaki türlerdeki ses ve video dosyalarını yükleyebilirsiniz:

  • Yerel dosyalar: Çok parçalı yükleme kullanılır. Tek bir dosyanın maksimum boyutu 48,8 TB'dir. Kaldığı yerden devam ettirilebilir yükleme desteklenmez. Daha fazla bilgi için örnek kodda bulunan testUploadLocalVideo işlevine bakın.

  • Web dosyaları: Yükleme için bir dosya URL'si belirtebilirsiniz. Tek bir dosyanın maksimum boyutu 48,8 TB'dir. Bu yöntem, web dosyasının yüklenmeden önce yerel diske indirilmesini gerektirir. Yerel diskinizde yeterli alan olduğundan emin olun. Daha fazla bilgi için örnek kodda bulunan testUploadWebVideo işlevine bakın.

Örnek kodu görüntülemek için tıklayın

# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest 
# Test uploading a local audio or video file
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
    try:
        # You can specify the region where the upload script is deployed. If the ECS region is the same as the ApsaraVideo VOD storage region, the upload is automatically performed over the internal network. This is faster and saves Internet traffic.
        # ecsRegionId ="cn-shanghai"
        # uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
        # Do not specify the ECS region where the upload script is deployed.
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(filePath, 'exampleTitle')
        # You can set a video thumbnail. If it is a local or web image, you can use UploadImageRequest to upload the image to ApsaraVideo VOD and obtain the ImageURL.
        # ImageURL example: https://example.com/sample-****.jpg
        #uploadVideoRequest.setCoverURL('<your Image URL>')  
        # Tags
        #uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadLocalVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
    except AliyunVodException as e:
        print(e)
# Test uploading a web audio or video file
def testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl, storageLocation=None):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(fileUrl, 'exampleTitle')
        uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadWebVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
    except AliyunVodException as e:
        print(e)
####  Run the test code   ####   
# The AccessKey of an Alibaba Cloud account has permissions on all APIs. We recommend that you use the AccessKey of a RAM user for API calls or routine O&M.
# Do not store your AccessKey ID and AccessKey secret in your project code. This can lead to a leak of your AccessKey and compromise the security of all resources under your account.
# This example reads the AccessKey from environment variables for identity verification. Before you run the sample code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
accessKeyId = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
accessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
localFilePath = '/opt/video/sample-****.mp4'
testUploadLocalVideo(accessKeyId, accessKeySecret, localFilePath)
# fileURL example: http://video.developer.aliyundoc.com/video/sample-****.mp4
fileUrl = '<your file URL>'
#testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)

M3U8 dosyalarını yükleme

Örnek kodu görüntülemek için tıklayın

# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest
# Test uploading a local M3U8 video
def testUploadLocalM3u8(accessKeyId, accessKeySecret, m3u8LocalFile):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8LocalFile, 'exampleTitle')
        # uploadVideoRequest.setTemplateGroupId('<TemplateGroupId>')
        # The segment files and the M3U8 file are in the same directory. The SDK automatically parses and uploads them.
        videoId = uploader.uploadLocalM3u8(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
    except AliyunVodException as e:
        print(e)
# Test uploading a web M3U8 video
def testUploadWebM3u8(accessKeyId, accessKeySecret, m3u8FileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8FileUrl, 'exampleTitle')
        # Parse the segment file URLs. This applies when the segment URLs have the same signature as the M3U8 file or have no signature. In other cases, you need to parse them yourself.
        sliceFileUrls = uploader.parseWebM3u8(m3u8FileUrl)
        videoId = uploader.uploadWebM3u8(uploadVideoRequest, sliceFileUrls)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
    except AliyunVodException as e:
        print(e)
####  Run the test code   ####
# The AccessKey of an Alibaba Cloud account has permissions on all APIs. We recommend that you use the AccessKey of a RAM user for API calls or routine O&M.
# Do not store your AccessKey ID and AccessKey secret in your project code. This can lead to a leak of your AccessKey and compromise the security of all resources under your account.
# This example reads the AccessKey from environment variables for identity verification. Before you run the sample code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
accessKeyId = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
accessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
m3u8LocalFile = '/opt/video/m3u8/example_01.m3u8'
#testUploadLocalM3u8(accessKeyId, accessKeySecret, m3u8LocalFile)
m3u8FileUrl = 'http://sample.oss.aliyuncs.com/video/m3u8/example****.m3u8'
#testUploadWebM3u8(accessKeyId, accessKeySecret, m3u8FileUrl)
            

Yükleme hızlandırma

Büyük dosyaları (GB veya TB seviyesinde) yüklemek veya Çin anakarasından Singapur depolama bölgesindeki bir depolama adresine video yüklemek gibi çapraz bölge yüklemeleri gerçekleştirmek için yükleme hızlandırma özelliğini etkinleştirebilirsiniz. Daha fazla bilgi için bkz. Yükleme hızlandırmayı etkinleştirme. Bu özelliği etkinleştirdikten sonra yükleme yapılandırmasındaki UserData nesnesine anahtar-değer çifti olarak AccelerateConfig eklemeniz gerekir. UserData nesnesi yoksa bunu oluşturmanız gerekir. Aşağıda bir örnek verilmiştir:

uploadVideoRequest.setUserData("{\"AccelerateConfig\":{\"Type\":\"oss\",\"Domain\":\"****Bucket.oss-accelerate.aliyuncs.com\"}}");

Parametre açıklaması

Ad

Tür

Açıklama

Type

string

Etkinleştirilecek yükleme hızlandırma türü. Yalnızca oss desteklenir.

Domain

string

Paketinizin hızlandırılmış alan adı. Varsayılan protokol HTTPS'dir.

Not

Etkinleştirmeden sonra atanan bir hızlandırılmış alan adı kullanın, örneğin vod-***.oss-accelerate.aliyuncs.com.

Senaryo 2: Görüntüleri yükleme

Örnek kodu görüntülemek için tıklayın

# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadImageRequest import UploadImageRequest
# Test uploading a local image
def testUploadLocalImage(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(filePath)
        uploadImageRequest.setTitle('exampleTitle')  # Set the image title. The default value is empty.
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, True)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))
    except AliyunVodException as e:
        print(e)
# Test uploading a web image
def testUploadWebImage(accessKeyId, accessKeySecret, fileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(fileUrl)
        uploadImageRequest.setTitle('exampleTitle')  # Set the image title. The default value is empty.
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, False)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))
    except AliyunVodException as e:
        print(e)
####  Run the test code   ####
# The AccessKey of an Alibaba Cloud account has permissions on all APIs. We recommend that you use the AccessKey of a RAM user for API calls or routine O&M.
# Do not store your AccessKey ID and AccessKey secret in your project code. This can lead to a leak of your AccessKey and compromise the security of all resources under your account.
# This example reads the AccessKey from environment variables for identity verification. Before you run the sample code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
accessKeyId = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
accessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
localFilePath = '/opt/image/image01.png'
#testUploadLocalImage(accessKeyId, accessKeySecret, localFilePath)
fileUrl = 'http://vod-download.cn-shanghai.aliyuncs.com/retina/pic/20180208/496AE240-54AE-4CC8-8578-3EEC8F38****.gif'
testUploadWebImage(accessKeyId, accessKeySecret, fileUrl)
            

Senaryo 3: Yardımcı medya varlıklarını yükleme

Örnek kodu görüntülemek için tıklayın

# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadAttachedMediaRequest import UploadAttachedMediaRequest
# Test uploading a local auxiliary media asset (such as a watermark or caption file)
def testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadAttachedRequest = UploadAttachedMediaRequest(filePath, 'watermark')
        uploadAttachedRequest.setTitle('exampleTitle')
        media = uploader.uploadAttachedMedia(uploadAttachedRequest, True)
        print(media)
    except AliyunVodException as e:
        print(e)
# Test uploading a web auxiliary media asset (such as a watermark or caption file)
def testUploadWebAttachedMedia(accessKeyId, accessKeySecret, fileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadAttachedRequest = UploadAttachedMediaRequest(fileUrl, 'watermark')
        uploadAttachedRequest.setTitle('exampletitle')
        media = uploader.uploadAttachedMedia(uploadAttachedRequest, False)
        print(media)
    except AliyunVodException as e:
        print(e)
####  Run the test code   ####
# The AccessKey of an Alibaba Cloud account has permissions on all APIs. We recommend that you use the AccessKey of a RAM user for API calls or routine O&M.
# Do not store your AccessKey ID and AccessKey secret in your project code. This can lead to a leak of your AccessKey and compromise the security of all resources under your account.
# This example reads the AccessKey from environment variables for identity verification. Before you run the sample code, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
accessKeyId = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
accessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
localFilePath = '/opt/image/sample.png'
#testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, localFilePath)
fileUrl = 'http://vod-download.cn-shanghai.aliyuncs.com/retina/pic/20180208/496AE240-54AE-4CC8-8578-3EEC8F38****.gif'
testUploadWebAttachedMedia(accessKeyId, accessKeySecret, fileUrl)
            

SSS

Web video akışını yüklemek için örnek kodu kullanırken ERROR:root:Download fail: [Errno 2] No such file or directory hatası bildirilirse voduploadsdk klasörüyle aynı dizinde manuel olarak bir dlfiles klasörü oluşturarak sorunu çözebilirsiniz.

/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/venv/bin/python  /Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/uploadVideo.py
[2022-02-16 20:54:59]Download https://img.a...                              To /Users/fulin/Documents/Code/PythonProject/
/Users/fulin...                                                              : InsecureRequestWarning: Unverifi...
    warnings.warn(...)
[2022-02-16 20:54:59]Download fail: [Errno 2] No such file or directory: '/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/dlfiles/115...
Type: FileDownloadError, Code: Download File Error, Message: , HTTPStatus: NULL, RequestId: NULL
ERROR:root:Download fail: [Errno 2] No such file or directory: '/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/dlfiles/115310cd4d...
Process finished with exit code 0