Snaží nastaviť hlavičku ContentDisposition na PutObjectCommand výsledky v 403 zakázané

0

Otázka

Mám nahrať súbory, aby S3 úspešne mojej žiadosti. Ja priamy upload z prehliadača pomocou signedUrl, že môj server generuje pre mňa pomocou aws-sdk v3.

Ak chcete získať singed URL vyzerá to trochu ako tento

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;
    }
};

Funguje to úplne v poriadku, ale potom, ako som si niečo dokumentácie videl som, že by som mal byť schopný nastaviť hlavičku ContentDisposition. V tejto dokumentácii hovorí, že vstup PutObjectCommand siaha od PutObjectRequest

Tá má voliteľný parameter s názvom ContentDisposition ako by som chcel nastaviť túto prílohu, aby mi umožnilo urýchliť, "stiahnuť" okno pre moje používateľov. Avšak, keď som sa použiť signedURL ako je uvedené vyššie, ale pridať ContentDisposition:'attachment' pole mám Zakázané Chyba.

Vie niekto, či im chýba nič tu? nie je to skutočné možnosť, alebo musím niečo zmeniť v mojom povolenia S3 za to?

1

Najlepšiu odpoveď

1

Musíme špecifikovať ContentDisposition pre PutObjectCommand param a aj pre getSignedUrl funkcie ako:

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

V iných jazykoch

Táto stránka je v iných jazykoch

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