Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating an asset from upload fails #234

Open
paul-mesnilgrente opened this issue Nov 9, 2021 · 0 comments
Open

Creating an asset from upload fails #234

paul-mesnilgrente opened this issue Nov 9, 2021 · 0 comments

Comments

@paul-mesnilgrente
Copy link

paul-mesnilgrente commented Nov 9, 2021

Hi,

I have this code snippet:

client = Contentful::Management::Client.new(ENV['CONTENTFUL_ACCESS_TOKEN'])
space = client.spaces.find(ENV['CONTENTFUL_SPACE_ID'])
environment = client.environments(space.id).find('master')

upload = client.uploads(space.id).create(image.image.path)
# create the associated file
file = Contentful::Management::File.new
file.properties[:contentType] = image.mime_type
file.properties[:fileName] = image.image_name
file.properties[:uploadFrom] = upload.to_link_json

# create the asset with the associated file/upload
asset = client.assets(space.id, environment.id).create(
  title_with_locales: { 'en-NZ' => image.title },
  file_with_locales:  { 'en-NZ' => file }
)
asset.process_file

which is pretty much the example from the README.md

However this doesn't work. The created asset is "Untitled" with no attachment (looking at the Contentful UI).

I tried this fix:

asset = client.assets(space.id, environment.id).create(
  title_with_locales: { 'en-NZ' => image.title },
  file_with_locales:  { 'en-NZ' => file }
)
asset = environment.assets.find(asset.id)
asset.title_with_locales       = { 'en-NZ' => image.title }
asset.description_with_locales = { 'en-NZ' => image.image_alt }
asset.file_with_locales =        { 'en-NZ' => file }

asset.save
asset.process_file

which is renaming the asset and linking the upload. Do you know why I need to set the attributes twice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant