Recreate Parse.File object from its URL

I have some URL strings of files that were originally constructed by saving data via the Parse.File API, but the Parse.File objects were not preserved. The data is still in the database (AWS, but this should not matter here) and accessible from the URLs.

Is there a way of recreating the Parse.File objects using these URL strings? AFAIK the API does not directly allow this: file objects apparently are either created from data or fetched from the db.

The only workaround I can think of is to 1) download each file URL, 2) re-upload the data to create a new file object, and 3) delete the old file URL. Should work but doesn’t seem especially elegant :nerd_face:

Any help appreciated :slight_smile:

  • What is the reason for uploading the same existing file again?
  • Did you make a server change? Do you want to provide access through a new URL?

Can you write a more explanatory reason?

Hey :slight_smile:

It’s not about re-uploading files, the file data is still there – as I have written in my original question: All I want is to recreate a ParseFile object using the still existing URL that points to the still existing data.

The only available public constructor of ParseFile is:

new ParseFile(name, data, type, metadata, tags)

However, I need something like this:

new ParseFile(url)

… where url is a URL to a file that already is present in the Parse server’s file store.

Obviously that’s not possible via the SDK. I’d be happy with a hacky way to achieve this since I only need it for a single migration task, where I plan to recreate file objects based on existing file store URLs, and save them in a property of a Parse class.

Best regards,
Martin

I ended up manually downloading and re-upload each file. Can’t believe that this is necessary just bc there is no way to construct a File object with existing data :frowning_face:

You can, I will give you some examples.


async reSave(filename) {
    const bucket = await this._getBucket();
    const documents = await bucket.find({
      filename
    }).toArray();

    if (documents.length === 0) {
      throw new Error('FileNotFound');
    }

    return Promise.all(documents.map(doc => {
doc.filename=“new”+doc.filename;
      return bucket.save(doc._id+’new’);
    }));
  }

You must get all file information (need name)

Your file looks like in db


{
    _id: ObjectId("**649b3ad968abdfa5c1465584new**"),
    length: 544,
    chunkSize: 261120,
    uploadDate: ISODate("2023-06-27T19:42:17.276Z"),
    filename: '**newbd68fb811abb302ee0ed53ec9b9b86c3**_setup.bin',
    metadata: {}
  }

You should save this file which class property used this.

db.getCollection("_User").updateOne({filter},)
{
  _id: 'SUs7ugUKPk',
  username: 'test',
  ...
  f: '**newbd68fb811abb302ee0ed53ec9b9b86c3**_setup.bin'
})

You should modify GridFSBucketAdapter file in Parse Server.

You can hack like this way.

Thank you :pray: That’s very good to know, even if it’s too late for my specific problem :slight_smile: Much appreciated!

1 Like