0

阿里云点播服务报错InvalidStorage.NotFound The storageLocation does not exit解决...

 2 years ago
source link: https://foofish.net/aliyun-vod-InvalidStorage.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

阿里云点播服务报错InvalidStorage.NotFound The storageLocation does not exit解决方案

By 刘志军, 2021-11-25, 分类: Python

阿里云

最近业务系统需要用到阿里云的视频点播服务,视频由前端上传,上传前需要业务服务器通过阿里云获取上传地址和上传凭证来保证安全性。

p183800.png

我们使用的RAM子账号来访问视频点播服务。 按照官方文档把代码撸出来,权限配置都确认没问题,程序始终跑不通,报一个错误 InvalidStorage.NotFound The storageLocation does not exit。

微信截图_20211125182052.png

根据文档说明,StorageLocation参数是非必填的,即使我强行设置还是一样报错

request.set_StorageLocation(xxxxx)

最后找到他们客服,给了我一个重要的线索,发现他们的功能是可以在线调试,还能生成代码。在线一调,找到了问题,比对他们的代码,发现少了一行重要的代码

request.set_endpoint("vod.cn-shenzhen.aliyuncs.com") #  这个值取决于你的存在放在哪个区域。我的是华南深圳区

就是这个endpoint参数其实是必填的,然而在文档里并没有对这个参数做任何说明,被他们坑死, 最后加上这行代码后终于跑通了。

完整代码:

"""
阿里云点播服务

参考文档:
上传地址和凭证:https://help.aliyun.com/document_detail/55397.htm?spm=a2c4g.11186623.0.0.48851fe4pFBLng#topic-1959332
"""
import json

from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkcore.client import AcsClient
from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest


class VOD:

    def __init__(self, app=None):
        self.app = app
        self.service = None
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.app = app
        regionId = 'cn-shenzhen'  # 点播服务接入区域
        connectTimeout = 3  # 连接超时,单位为秒
        credential = StsTokenCredential(self.app.config.get("ALIYUN_VOD_ACCESSKEYID"),
                                        self.app.config.get("ALIYUN_VOD_ACCESSKEYSECRET"),
                                        "123")
        self.client = AcsClient(region_id=regionId,
                                auto_retry=True,
                                max_retry_time=3,
                                timeout=connectTimeout,
                                credential=credential)

    def create_upload_video(self, title=None, filename=None, description=""):
        """
        获取视频上传地址和凭证
        """
        request = CreateUploadVideoRequest.CreateUploadVideoRequest()
        request.set_Title(title)
        request.set_endpoint("vod.cn-shenzhen.aliyuncs.com") # 这一行不能少,否则包StorageLocation not exsit。
        request.set_FileName(filename)
        request.set_Description(description)
        request.set_accept_format('JSON')
        response = json.loads(self.client.do_action_with_exception(request))
        return response

我是把这个服务当作一个Flask的extend来写的,如果不是基于flask的,可以像下面这样写。

credential = StsTokenCredential(ALIYUN_VOD_ACCESSKEYID,
                                ALIYUN_VOD_ACCESSKEYSECRET),
                                "123") # 123 是token,随便指定
client = AcsClient(region_id=regionId,
                        auto_retry=True,
                        max_retry_time=3,
                        timeout=connectTimeout,
                        credential=credential)

request = CreateUploadVideoRequest.CreateUploadVideoRequest()
request.set_Title(title)
request.set_endpoint("vod.cn-shenzhen.aliyuncs.com") # 这一行不能少,否则包StorageLocation not exsit。
request.set_FileName(filename)
request.set_Description(description)
request.set_accept_format('JSON')
response = json.loads(client.do_action_with_exception(request))

有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK