Cercando di impostare intestazione ContentDisposition su PutObjectCommand risultati in un 403 forbidden

0

Domanda

Io caricare i file su S3 con successo con la mia domanda. Faccio un upload diretto dal browser utilizzando un signedUrl che il mio server genera per me usando il aws-sdk v3.

Per ottenere il cantato URL sembra un po ' come questo

const s3Params = {
        Bucket : bucketName,
        Key : fileName,
        ContentType:fileType,
        // Metadata:{'Content-Disposition':'attachment'}
        // ContentDisposition:'attachment'
    };


    try {
        const command = new PutObjectCommand(s3Params);

        const url = await getSignedUrl(s3v3,command,{expiresIn:60});
        return url;
    } catch (e) {
        console.log('************** there was an error signing th url');
        console.log(e);
        throw e;
    }
};

Questo funziona alla perfezione, ma poi, come ho letto un po ' di documentazione ho visto che dovrei essere in grado di impostare l'intestazione ContentDisposition. In questa documentazione si dice che l'ingresso di PutObjectCommand si estende dal PutObjectRequest

Quest'ultimo è un parametro opzionale chiamato ContentDisposition come vorrei impostare questo allegato, per consentire a me di aprire un finestra "download" per i miei utenti. Tuttavia, quando uso la signedURL come sopra, ma aggiungere il ContentDisposition:'attachment' campo ottengo un Errore Forbidden.

Qualcuno sa se im manca qualcosa qui? non è questa una reale opzione o devo modificare qualcosa nel mio autorizzazioni di S3 per questo?

1

Migliore risposta

1

Dobbiamo specificare il ContentDisposition per il PutObjectCommand param e anche per il getSignedUrl funzione in quanto tale:

async function main(fileName, bucketName, fileType) {
    const s3Params = {
        Bucket: bucketName,
        Key: fileName,
        ContentType: fileType,
        ContentDisposition: 'attachment'
    };

    const client = new S3Client({region: 'us-east-1'});
    const command = new PutObjectCommand(s3Params);

    const url = await getSignedUrl(client, command, {expiresIn: 60, ContentDisposition: 'attachment'});

    const file = await fs.readFile(fileName);

    const result = await axios({
        method: 'put',
        url,
        data: file,
        headers: {
            'Content-Type': fileType,
            'Content-Disposition': 'attachment'
        }
    });

    return result;
}
2021-10-30 20:29:21

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................