【问题标题】:Cannot publish or process asset to contentful after creation创建后无法将资产发布或处理为内容
【发布时间】:2020-09-20 22:24:34
【问题描述】:

我正在为 Contentful Content Management API 使用 Python 客户端,目前无法处理或发布资产。在每种情况下,API 都会返回:

contentful_management.errors. NotFoundError: HTTP status code: 404 Message: The resource could not be found. Details: The requested Asset could not be found.

创建资产后,我可以访问它的 ID,因此假设那时我应该能够处理和发布。我的代码如下。有谁知道我可能做错了什么?

"""
Creating an asset requires three steps and API calls:

1. Create an asset.
2. Process an asset.
3. Publish an asset.
"""

def create_asset(self, path):
    p = os.path.abspath(path)
    return self.authorised_user.uploads(self.test_space_id).create(p)

def process_asset(self, asset_id):
    asset = self.authorised_user.assets(self.test_space_id, self.test_environment_id).find(asset_id)
    asset.process()

def publish_asset(self, asset_id):
    asset = self.authorised_user.assets(self.test_space_id, self.test_environment_id).find(asset_id)
    asset.publish()

【问题讨论】:

    标签: python contentful contentful-management


    【解决方案1】:

    您正在上传文件,但未在 create_asset 函数中创建资产。这是执行所有步骤的代码:

    import os
    import time
    from contentful_management import Client
    
    client = Client(os.environ['CMA_TOKEN'])
    space_id = 'space_id'
    environment_id = 'master'
    title = 'Title'
    file_to_upload = '/path/to/file/image.jpeg'
    fileName = os.path.basename
    contentType = 'image/jpeg'
    
    new_upload = client.uploads(space_id).create(file_to_upload)
    
    file_attributes = {
        'fields': {
            'title': {
                'en-US': title
            },
            'file': {
                'en-US': {
                    'fileName': fileName,
                    'contentType': contentType,
                    'uploadFrom': new_upload.to_link().to_json()
                }
            }
        }
    }
    
    new_asset = client.assets(space_id, environment_id).create(
       None, # change this to a string if you want to specify the asset ID
       file_attributes
     )
    
    new_asset.process()
    
    # it might take a few seconds for the asset processing to complete
    while new_asset.url is None:
        time.sleep(1)
        new_asset.reload()
    
    new_asset.publish()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多