-
-
Save timurvafin/7234eed89cc0aa219141e4a27c9f2ccb to your computer and use it in GitHub Desktop.
Payload s3 presigned
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
S3_ACCESS_KEY_ID= | |
S3_SECRET_ACCESS_KEY= | |
S3_BUCKET=ncip-pg-dev | |
S3_ENDPOINT=https://storage.yandexcloud.net | |
S3_REGION=ru-central1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { withPayload } from '@payloadcms/next/withPayload' | |
/** @type {import('next').NextConfig} */ | |
const nextConfig = { | |
images: { | |
remotePatterns: [ | |
{ | |
protocol: 'https', | |
hostname: 'storage.yandexcloud.net', | |
pathname: `/${process.env.S3_BUCKET}/**`, | |
}, | |
], | |
}, | |
} | |
export default withPayload(nextConfig) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { s3Storage } from '@payloadcms/storage-s3' | |
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3' | |
import { getSignedUrl } from '@aws-sdk/s3-request-presigner' | |
const bucket = process.env.S3_BUCKET || '' | |
const accessKeyId = process.env.S3_ACCESS_KEY_ID || '' | |
const secretAccessKey = process.env.S3_SECRET_ACCESS_KEY || '' | |
const endpoint = process.env.S3_ENDPOINT || '' | |
const region = process.env.S3_REGION || '' | |
const s3Configuration = { | |
credentials: { accessKeyId, secretAccessKey }, | |
endpoint: endpoint, | |
region: region, | |
forcePathStyle: true, | |
} | |
const getPresignedUrl = async ({ prefix, filename }: { prefix?: string | undefined; filename: string }): Promise<string> => { | |
const command = new GetObjectCommand({ | |
Bucket: bucket, | |
Key: `${prefix}/${filename}`, | |
}) | |
const s3Client = new S3Client(s3Configuration) | |
return getSignedUrl(s3Client, command, { expiresIn: 3600 }) | |
} | |
export const s3StoragePlugin = s3Storage({ | |
acl: 'public-read', | |
collections: { | |
media: { prefix: 'media', generateFileURL: getPresignedUrl }, | |
'document-pages': { prefix: 'document-pages', generateFileURL: getPresignedUrl }, | |
}, | |
bucket: bucket, | |
config: s3Configuration, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment