Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use aws package to upload to s3 #5131

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/mixpanel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"exports": {
"./*": "./src/*.ts"
},
"dependencies": {
"mixpanel": "^0.18.0"
}
}
32 changes: 10 additions & 22 deletions packages/scoutgame-ui/src/actions/createUserClaimScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { S3Client, type PutObjectCommandInput } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { log } from '@charmverse/core/log';
import { prisma } from '@charmverse/core/prisma-client';
import { getS3ClientConfig } from '@packages/aws/getS3ClientConfig';
import { uploadFileToS3 } from '@packages/aws/uploadToS3Server';
import { getLastWeek } from '@packages/scoutgame/dates';
import { getClaimablePointsWithSources } from '@packages/scoutgame/points/getClaimablePointsWithSources';
import { baseUrl } from '@packages/utils/constants';
import puppeteer from 'puppeteer';
import React from 'react';

import { PointsClaimBuilderScreen } from '../components/claim/components/PointsClaimScreen/PointsClaimModal/PointsClaimBuilderScreen';
import { PointsClaimScoutScreen } from '../components/claim/components/PointsClaimScreen/PointsClaimModal/PointsClaimScoutScreen';

const client = new S3Client(getS3ClientConfig());

export async function createUserClaimScreen({ userId, week }: { userId: string; week: string }) {
export async function createUserClaimScreen(userId: string) {
const { renderToString } = await import('react-dom/server');

const user = await prisma.scout.findUniqueOrThrow({
Expand Down Expand Up @@ -93,26 +89,18 @@ export async function createUserClaimScreen({ userId, week }: { userId: string;
await page.waitForNetworkIdle();

const screenshot = await page.screenshot();
const bucket = process.env.S3_UPLOAD_BUCKET;

const params: PutObjectCommandInput = {
Bucket: bucket,
Key: `points-claim/${userId}/${week}.png`,
Body: screenshot,
ContentType: 'image/png'
};

const s3Upload = new Upload({
client,
params
await uploadFileToS3({
pathInS3: `points-claim/${userId}/${getLastWeek()}.png`,
bucket: process.env.S3_UPLOAD_BUCKET,
content: screenshot as Buffer,
contentType: 'image/png'
});

await s3Upload.done();
log.info('generated claim creen', { claimedPoints, bucket, week, userId });
log.info('generated claim screen', { userId });
} catch (e) {
log.error('error generating claim screen', { userId, week, claimedPoints, error: e });
log.error('error generating claim screen', { userId, error: e });
} finally {
await browser.close();
}
return { week };
}
51 changes: 12 additions & 39 deletions packages/scoutgame/src/builderNfts/artwork/uploadMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/**
* OpenSea Metadata Specification Type
* Represents the metadata schema used for OpenSea items.
*
* @docs https://docs.opensea.io/docs/metadata-standards
*/
import type { PutObjectCommandInput, S3ClientConfig } from '@aws-sdk/client-s3';
import { S3Client } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { uploadFileToS3 } from '@packages/aws/uploadToS3Server';

import { getBuilderStarterPackContractAddress } from '../constants';

import { builderNftArtworkContractName } from './constants';
import { getNftTokenUrlPath, imageDomain } from './utils';

/**
* OpenSea Metadata Specification Type
* Represents the metadata schema used for OpenSea items.
*
* @docs https://docs.opensea.io/docs/metadata-standards
*/
type OpenSeaMetadata = {
/**
* URL to the image of the item.
Expand Down Expand Up @@ -83,22 +81,6 @@ type OpenSeaMetadata = {
youtube_url?: string;
};

function getS3ClientConfig() {
const config: Pick<S3ClientConfig, 'region' | 'credentials'> = {
region: process.env.S3_UPLOAD_REGION
};

if (process.env.S3_UPLOAD_KEY && process.env.S3_UPLOAD_SECRET) {
config.credentials = {
accessKeyId: process.env.S3_UPLOAD_KEY as string,
secretAccessKey: process.env.S3_UPLOAD_SECRET as string
};
}
return config;
}

const client = new S3Client(getS3ClientConfig());

/**
* Uploads OpenSea metadata to S3.
*
Expand Down Expand Up @@ -151,21 +133,12 @@ export async function uploadMetadata({
// Convert metadata to JSON buffer
const metadataBuffer = Buffer.from(JSON.stringify(metadata));

// Set up the S3 upload parameters
const params: PutObjectCommandInput = {
ACL: 'public-read',
Bucket: process.env.SCOUTGAME_S3_BUCKET,
Key: `nft/${metadataPath}`,
Body: metadataBuffer,
ContentType: 'application/json'
};

const s3Upload = new Upload({
client,
params
await uploadFileToS3({
pathInS3: `nft/${metadataPath}`,
bucket: process.env.SCOUTGAME_S3_BUCKET,
content: metadataBuffer,
contentType: 'application/json'
});

await s3Upload.done();

return `${imageDomain}/${metadataPath}`;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { S3Client } from '@aws-sdk/client-s3';
import type { PutObjectCommandInput, S3ClientConfig } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { uploadFileToS3 } from '@packages/aws/uploadToS3Server';

import { getBuilderActivities } from '../../../builders/getBuilderActivities';
import { getBuilderNft } from '../../../builders/getBuilderNft';
Expand All @@ -15,22 +13,6 @@ import {
updateNftStarterPackImage
} from './generateStarterPackNftImage';

function getS3ClientConfig() {
const config: Pick<S3ClientConfig, 'region' | 'credentials'> = {
region: process.env.S3_UPLOAD_REGION
};

if (process.env.S3_UPLOAD_KEY && process.env.S3_UPLOAD_SECRET) {
config.credentials = {
accessKeyId: process.env.S3_UPLOAD_KEY as string,
secretAccessKey: process.env.S3_UPLOAD_SECRET as string
};
}
return config;
}

const client = new S3Client(getS3ClientConfig());

export async function uploadStarterPackArtwork({
imageHostingBaseUrl,
avatar,
Expand Down Expand Up @@ -64,21 +46,13 @@ export async function uploadStarterPackArtwork({
contractName: getBuilderStarterPackContractAddress()
});

const params: PutObjectCommandInput = {
ACL: 'public-read',
Bucket: process.env.SCOUTGAME_S3_BUCKET,
Key: `nft/${imagePath}`,
Body: imageBuffer,
ContentType: 'image/png'
};

const s3Upload = new Upload({
client,
params
await uploadFileToS3({
pathInS3: `nft/${imagePath}`,
bucket: process.env.SCOUTGAME_S3_BUCKET,
content: imageBuffer,
contentType: 'image/png'
});

await s3Upload.done();

return `${imageDomain}/${imagePath}`;
}

Expand Down Expand Up @@ -116,20 +90,12 @@ export async function uploadStarterPackArtworkCongrats({
contractName: getBuilderStarterPackContractAddress()
});

const params: PutObjectCommandInput = {
ACL: 'public-read',
Bucket: process.env.SCOUTGAME_S3_BUCKET,
Key: `nft/${imagePath}`,
Body: imageBuffer,
ContentType: 'image/png'
};

const s3Upload = new Upload({
client,
params
await uploadFileToS3({
pathInS3: `nft/${imagePath}`,
bucket: process.env.SCOUTGAME_S3_BUCKET,
content: imageBuffer,
contentType: 'image/png'
});

await s3Upload.done();

return `${imageDomain}/${imagePath}`;
}
Loading