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

How to add file name when calling uploadFileGroup? #474

Open
NafisRubio opened this issue Apr 18, 2023 · 2 comments
Open

How to add file name when calling uploadFileGroup? #474

NafisRubio opened this issue Apr 18, 2023 · 2 comments
Labels
question Further information is requested

Comments

@NafisRubio
Copy link

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 NafisRubio added the question Further information is requested label Apr 18, 2023
@nd0ut
Copy link
Member

nd0ut commented Apr 18, 2023

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 :)

@optlsnd
Copy link

optlsnd commented Apr 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

3 participants