Gestion des fichiers
Pour manipuler un fichier (upload, write ou download), vous devez générer une PreSignUrl.
Vous pouvez transmettre la PreSignUrl à votre client en toute sécurité pour lui permettre d'uploader, d'écrire ou de télécharger un fichier depuis votre stockage.
Lors de l'upload ou de l'écriture d'un fichier, une nouvelle miniature est générée automatiquement. Vous pouvez désactiver ce comportement en passant should_not_generate_thumbnail=true en Query params (second paramètre de la fonction uploadApplicationFile).
Upload d'un fichier
// Generate a preSignUrl
const resultPreSignUrl = await getPreSignedUrlApplicationFile(
{ applicationId: "my-application-id" },
{
type: PreSignUrlType.UPLOAD,
fileName: fileToUpload.name,
},
);
const transfer = new Transfer({
name: "My Transfer",
type: TransferType.UPLOAD,
});
transfer.setAction(() => {
// Set any action to trigger on Upload/Download Progression
});
// Use the PreSignUrl to upload the file
await uploadApplicationFile(
{ token: resultPreSignUrl.data.preSignUrl },
{},
file,
{ transfer },
);
Écriture d'un fichier
// Generate a preSignUrl
const resultPreSignUrl = await getPreSignedUrlApplicationFile(
{
applicationId: "my-application-id",
applicationFileId: "file-id"
},
{
type: PreSignUrlType.WRITE,
},
);
const transfer = new Transfer({
name: "My Transfer",
type: TransferType.UPLOAD,
});
transfer.setAction(() => {
// Set any action to trigger on Upload/Download Progression
});
// Use the PreSignUrl to write the new file
await writeApplicationFile(
{ token: resultPreSignUrl.data.preSignUrl },
{},
file,
{ transfer },
);
Téléchargement d'un fichier
// Generate a preSignUrl
const resultPreSignUrl = await getPreSignedUrlApplicationFile(
{
applicationId: "my-application-id",
},
{
type: PreSignUrlType.DOWNLOAD,
userApplicationFileId: userApplicationFile.id,
},
);
const transfer = new Transfer({
name: "My Transfer",
type: TransferType.DOWNLOAD,
});
transfer.setAction(() => {
// Set any action to trigger on Upload/Download Progression
});
// Use the PreSignUrl to downloadthe new file
const result = await downloadApplicationFile(
{ token: resultPreSignUrl.data.preSignUrl },
{ transfer },
);
Publier un fichier
Chaque fichier est accessible via PreSignUrl, mais vous pouvez aussi activer un lien d'accès public pour permettre à n'importe qui de télécharger votre fichier, par exemple comme fournisseur d'images pour votre site web.
// By reseting the PublicAccess, you can also enable it
await resetPublicAccessApplicationFile({
applicationId: "application-id",
applicationFileId: "application-file-id",
});
// You can also disable this feature by unsetting the PublicAccess
await unsetPublicAccessApplicationFile({
applicationId: "application-id",
applicationFileId: "application-file-id",
});
Vous pouvez télécharger un fichier (sans authentification) via le PublicAccess avec la méthode downloadWithPublicAccessApplicationFile
Upload depuis un lien externe
Vous pouvez fournir un lien pointant vers un fichier pour l'uploader vers votre stockage.
await uploadFromUrlApplicationFile(
{ applicationId: "application-id" },
{
name: "filename.png",
url: "https://some-url.com/file/download",
},
);