uploadcare-js-api-clients icon indicating copy to clipboard operation
uploadcare-js-api-clients copied to clipboard

How to add file name when calling uploadFileGroup?

Open NafisRubio opened this issue 1 year ago • 2 comments

Question

When uploading an array of images by url, how to add custom metadata do each image, like file name, tags, labels?

import {uploadFileGroup} from '@uploadcare/upload-client'

const urls = [
  'https://example.com/903948384271/image.png',
  'https://example.com/903948384282/image.png',
  'https://example.com/903948384293/image.png',
  'https://example.com/903948384302/image.png',
  'https://example.com/903948384313/image.png',
]

const {files} = await uploadFileGroup(urls, {
  publicKey: 'uploadCarePublicKey',
  store: 'auto',
  checkForUrlDuplicates: true,
  secureSignature: 'secureSignature',
  secureExpire: 'secureExpire',
  maxConcurrentRequests: 10,
  saveUrlForRecurrentUploads: true
})
// RESULT
files = [
  {
    mimeType: 'image/jpeg',
    name: 'image.png',
    originalFilename: 'image.png',
  },
  {
    mimeType: 'image/jpeg',
    name: 'image.png',
    originalFilename: 'image.png',
  },
  {
    mimeType: 'image/jpeg',
    name: 'image.png',
    originalFilename: 'image.png',
  },
  {
    mimeType: 'image/jpeg',
    name: 'image.png',
    originalFilename: 'image.png',
  },
  {
    mimeType: 'image/jpeg',
    name: 'image.png',
    originalFilename: 'image.png',
  },
]

NafisRubio avatar Apr 18 '23 07:04 NafisRubio

Hey @NafisRubio,

To set unique metadata per each file, you need to upload each file separately and then create a group from the list of UUIDs.

import { uploadFile, uploadFileGroup } from "@uploadcare/upload-client";

const urls = [
  "https://example.com/903948384271/image.png",
  "https://example.com/903948384282/image.png",
  "https://example.com/903948384293/image.png",
  "https://example.com/903948384302/image.png",
  "https://example.com/903948384313/image.png",
];

const files = await Promise.all(
  urls.map((url) =>
    uploadFile(url, {
      publicKey: "demopublickey",
      metadata: { foo: "bar" },
      fileName: "image.png",
    })
  )
);

const group = await uploadFileGroup(
  files.map((file) => file.uuid),
  { publicKey: "demopublickey" }
);

Also, I want to note that maxConcurrentRequests only affects multipart uploads. Unfortunately, this is not mentioned in the documentation.

Furthermore, we are missing a method for batched file uploads with concurrency, perhaps we should add it.

I also noticed that metadata property cannot be passed to uploadFileGroup, this is a bug which we will fix soon, thank you :)

nd0ut avatar Apr 18 '23 08:04 nd0ut

Hi @NafisRubio, here's a live demo of the above solution suggested by @nd0ut. Check it out https://codesandbox.io/s/funny-snow-6lcrv9?file=/src/index.js

optlsnd avatar Apr 18 '23 08:04 optlsnd