Skip to main content

Files

Get file info

You can get file info by calling the getFileInfo method. It returns the basic metadata informaton of a file.

let bucketName = "profile-images";
let fileName = "rooby-avatar.png";

// Returns the metadata information about the file
let result = await altogic.storage.bucket(bucketName).file(fileName).getInfo();
Example response
{
"data": {
"_id": "6237419e161326736e4ffde3",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/6237419e161326736e4ffde3",
"isPublic": false,
"uploadedAt": "2022-03-20T15:00:46.275Z",
"updatedAt": "2022-03-20T16:14:29.332Z",
"userId": "62c14524cf074016b1045366",
"tags": []
},
"errors": null
}
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Check whether file exists

You can check whether the file exists by calling the exists method. It checks whether the file exists or not and returns true if file exists, otherwise false .

let bucketName = "profile-images";

// Checks whether the `rooby-avatar.png` file exists
// in `profile-images` bucket or not
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.exists();
Example response
{
"data": true,
"errors": null
}
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Download

You can download the file by calling the download method. It downloads the file and returns the file content Blob.

let fileName = "rooby-avatar.png";

// Downloads the file
let result = await altogic.storage
.bucket("profile-images")
.file(fileName)
.download();
Example response
{
"data": Blob { size: 1024, type: "image/jpeg" },
"errors": null
}
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Duplicate

You can duplicate the file by calling the duplicate method. It duplicates the file and returns the new file instance.

let bucketName = "profile-images";
let fileName = "rooby-avatar.png";
let duplicateName = "rooby-avatar-copy.png";

// Duplicates the `rooby-avatar.png` file in `profile-images` bucket
// and creates a new file with name `rooby-avatar-copy.png`
let result = await altogic.storage
.bucket(bucketName)
.file(fileName)
.duplicate(duplicateName);
Example response
{
"data": {
"_id": "62379bbd0b72592107523e49",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar-copy.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379bbd0b72592107523e49",
"isPublic": false,
"uploadedAt": "2022-03-20T21:25:17.911Z",
"updatedAt": "2022-03-20T21:25:17.911Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}

Parameters

Here you can find parameters for the duplicate method.

#

Name

Data type

Required

Description

1duplicateNamestringNoThe new duplicate file name. If not specified, uses the fileName as template and ensures the duplicated file name to be unique in its bucket.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Copy file to bucket

You can copy the file to another bucket by calling the copyTo method. If there already exists a file with the same name in destination bucket, it ensures the copied file name to be unique in its new destination.

let bucketName = "profile-images";
let bucketNameOrId = "employee-images";

// Copies the file to another bucket
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.copyTo(bucketNameOrId);
Example response
{
"data": {
"_id": "62379cfb0b72592107523e4a",
"bucketId": "62379ce345aba7a695579b49",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62379ce345aba7a695579b49/62379cfb0b72592107523e4a",
"isPublic": false,
"uploadedAt": "2022-03-20T21:30:35.609Z",
"updatedAt": "2022-03-20T21:30:35.609Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}

Parameters

Here you can find parameters for the copyTo method.

#

Name

Data type

Required

Description

1bucketNameOrIdstringYesThe name or id of the bucket to copy the file into.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Move file to bucket

You can move the file to another bucket by calling the moveTo method. The file will be removed from its current bucket and will be moved to its new bucket. If there already exists a file with the same name in destination bucket, it ensures the moved file name to be unique in its new destination.

let bucketName = "profile-images";
let bucketNameOrId = "employee-images";

// Moves the file to another bucket
let result = await altogic.storage
.bucket(bucketName)
.file("rooby.png")
.moveTo(bucketNameOrId);
Example response
{
"data": {
"_id": "62379df1b9a84d607cd79016",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379df1b9a84d607cd79016",
"isPublic": false,
"uploadedAt": "2022-03-20T21:34:41.617Z",
"updatedAt": "2022-03-20T21:35:30.369Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}

Parameters

Here you can find parameters for the moveTo method.

#

Name

Data type

Required

Description

1bucketNameOrIdstringYesThe name or id of the bucket to move the file into.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Make file public

You can make the file public by calling the makePublic method. It changes the privacy of the file to true.

let bucketName = "profile-images";

// Makes the file public
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.makePublic();
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": true,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:37:01.932Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Make file private

You can make the file private by calling the makePrivate method. It changes the privacy of the file to false.

let bucketName = "profile-images";

// Makes the file private
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.makePrivate();
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": false,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:38:03.854Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Add tags to file

You can add your own tags to each uploaded file. This metadata can be used in expressions to filter files or store data specific to your application.

let bucketName = "profile-images";

let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.addTags(["picture", "user", "profile"]);
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": true,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:37:01.932Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality", "picture", "user", "profile"]
},
"errors": null
}

Parameters

Here you can find parameters for the addTags method.

#

Name

Data type

Required

Description

1tagsstring or string[]YesA single tag or an array of tags to add to file's metadata.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Remove tags from file

You can also remove tags (e.g., your custom metadata associated with your files) from your files.

let bucketName = "profile-images";

let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.removeTags(["picture", "user"]);
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": true,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:37:01.932Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality", "profile"]
},
"errors": null
}

Parameters

Here you can find parameters for the removeTags method.

#

Name

Data type

Required

Description

1tagsstring or string[]YesA single tag or an array of tags to remove from file's metadata.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Update file info

You can update the overall file information (name, isPublic and tags) in a single method call, instead of calling each individual update method separately.

let bucketName = "profile-images";

// Makes the `profile-images` bucket and contents public.
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.updateInfo("avagar.png", false, ["user", "profile"]);
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "rooby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": false,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:37:01.932Z",
"userId": "62c14524cf074016b1045366",
"tags": ["user", "profile"]
},
"errors": null
}

Parameters

Here you can find parameters for the updateInfo method.

#

Name

Data type

Required

Description

1newNamestringYesThe new name of the file.
2isPublicbooleanYesThe privacy setting of the file.
3tagsstring or string[]YesArray of string values that will be set as the file metadata.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Rename file

You can rename the file by calling the rename method. If the file name already exist in the bucket, returns an error.

let bucketName = "profile-images";
let newName = "booby-avatar.png";

// Renames the file
let result = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.rename(newName);
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "booby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": false,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:41:16.605Z",
"userId": "62c14524cf074016b1045366",
"tags": ["low-quality"]
},
"errors": null
}

Parameters

Here you can find parameters for the rename method.

#

Name

Data type

Required

Description

1newNamestringYesThe new name of the file.
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Replace file

You can replace the file by calling the replace method. It keeps the name of the file but replaces file contents, size, encoding and mime-type with the newly uploaded file info and returns the metadata of the file after replacement.

let bucketName = "profile-images";
const selectedFile = event.target.files[0];

// Replaces the file
let result = await altogic.storage
.bucket(bucketName)
.file("booby-avatar.png")
.replace(selectedFile);
Example response
{
"data": {
"_id": "62379ba045aba7a695579b48",
"bucketId": "62373bae161326736e4ffde2",
"fileName": "booby-avatar.png",
"size": 53951,
"encoding": "7bit",
"mimeType": "image/png",
"publicPath": "https://c1-na.altogic.com/_storage/6233230ba1c88fcb1ad5919a/62373bae161326736e4ffde2/62379ba045aba7a695579b48",
"isPublic": false,
"uploadedAt": "2022-03-20T21:24:48.514Z",
"updatedAt": "2022-03-20T21:41:16.605Z"
},
"errors": null
}

Parameters

Here you can find parameters for the replace method.

#

Name

Data type

Required

Description

1fileBodyanyYesThe body of the new file that will be used to replace the existing file.
2optionsFileUploadOptionsYesContent type of the file, privacy setting of the file, tags of the file and whether to create the bucket if not exists. contentType is ignored, if fileBody is Blob, File or FormData, otherwise contentType option needs to be specified.
If not specified, contentType will default to text/plain;charset=UTF-8.
If isPublic is not specified, defaults to the bucket's privacy setting. If createBucket is set to true (defaults to false), then creates a new bucket if the bucket does not exist.
info

If onProgress callback function is defined in FileUploadOptions, it periodically calls this function to inform about upload progress. Please note that for the moment onProgress callback function can only be used in clients where XMLHttpRequest object is available (e.g., browsers).

note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

Delete file

You can delete the file by calling the delete method. It deletes the file from the bucket.

let bucketName = "profile-images";

// Deletes the file from the bucket
let { errors } = await altogic.storage
.bucket(bucketName)
.file("rooby-avatar.png")
.delete();
note

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.