I have a model called “product” that comes in 5 enumerated “finishes”. Each product-finish combination is a separate row (am I doing this right?). I want to associate the product with a 3d model (STL). But I don’t want to upload the same STL to each finish, I want each finish to share the same media of the product. Is there a way to share media between items? Maybe there is a way to upload to the media library, and then get a URL for the media, and store the hyperlink instead of the STL itself? Or is there a better way to make variants of a “product” model that I don’t know about?
I am running Strapi connected to Azure Blob Storage to hold the media files. I will be creating product items programatically through the API.
All media files can be shared between any number of entities from any number of models. There is no restriction here.
How do I assign a media file to a content object when I post? URL?
Hi Steve,
Where you able to find a solution?
I haven’t had a chance to test it because I am still trying to upload any content at all through the API.
Hmmm maybe this snippet might help. In a nutshell, it uploads a png QR code image saved on disk and associates it with a QR code collection type in the Image field.
const setUpQRCodeUploadData = (qrCodeId) => ({ data: { refId: qrCodeId, ref: 'api::qr-code.qr-code', field: 'Image' } })
const setUpQRCodeUploadFiles = (filePath, slug) => ({ files: { path: filePath, name: url.slugCreator(slug) + '.png', type: 'image/png', size: fs.statSync(filePath).size } })
const uploadImageToStrapi = async (data, files) => {
try {
await strapi.service('plugin::upload.upload').upload({ ...data, ...files })
} catch (e) {
throw e
}
}
const uploadQRCodeImageToStrapi = async (qrCodeId, slug, filePath) => {
const uploadData = setUpQRCodeUploadData(qrCodeId)
const uploadFiles = setUpQRCodeUploadFiles(filePath, slug)
await uploadImageToStrapi(uploadData, uploadFiles)
}
It took me a while to figure out that the ref in the data
object has to be prefixed with api::
. Would this be, more or less, what you are trying to accomplish?
@braulio.balanza, @dmehaffy
I am now able to upload a file. But I am not sure how to link it to an item in a collection. I could take the URL of the media (I store it on Azure) and save it as a URL on the object. But that doesn’t seem right; it is a URL, not a media object, and I can only assign one, not a collection. How do I assign a previously-uploaded media file to a field of type media, assuming I have the data from the upload post response?
After reading the documentation, I think I have an idea of how this might work.
I don’t think I can upload the media and link it to a product. Instead, I can upload my STL in the process of creating an object called “Model-3D”. Upload - Strapi Developer DocsThen I make a “Product” item. Following the GraphQL example I can send a put request that will assign the Model-3d item to the Product item, calling the Product item as the target of the API and passing in the Model-3d id. That example is here: GraphQL API - Strapi Developer Docs]
Or, I can create the Model-3d object with attached binary, and then while creating the Product assign the Model-3D Id as a relation by passing in the Model-3D id.
Or am I over-complicating things? Can I upload media first, and assign the media’s ID to the product, even though the media is orphaned and not assigned to an item in a collection?
Just to circle back, my goal is to assign one media object to multiple Products, so I cannot upload the media while creating the product, because I don’t want a duplicate media for all the products.