Skip to content

Commit

Permalink
Switch to Linode Object Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mudkipme committed Oct 13, 2023
1 parent a82ec63 commit d92a5aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
- name: serverless deploy
uses: serverless/[email protected]
if: ${{ github.ref_name == 'dev' }}
Expand All @@ -36,3 +38,5 @@ jobs:
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
41 changes: 22 additions & 19 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ provider:
runtime: nodejs16.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'ap-northeast-1'}
iam:
role:
statements:
- Effect: "Allow"
Action:
- "s3:GetObject"
Resource: "arn:aws:s3:::${opt:bucket, 'media.52poke.com'}/*"
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
- "arn:aws:s3:::${opt:bucket, 'media.52poke.com'}/wiki/thumb/*"
- "arn:aws:s3:::${opt:bucket, 'media.52poke.com'}/webp-cache/*"
- Effect: "Allow"
Action:
- "s3:DeleteObject"
Resource: "arn:aws:s3:::${opt:bucket, 'media.52poke.com'}/webp-cache/*"
# iam:
# role:
# statements:
# - Effect: "Allow"
# Action:
# - "s3:GetObject"
# Resource: "arn:aws:s3:::${env:S3_BUCKET, 'media.52poke.com'}/*"
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# - "arn:aws:s3:::${env:S3_BUCKET, 'media.52poke.com'}/wiki/thumb/*"
# - "arn:aws:s3:::${env:S3_BUCKET, 'media.52poke.com'}/webp-cache/*"
# - Effect: "Allow"
# Action:
# - "s3:DeleteObject"
# Resource: "arn:aws:s3:::${env:S3_BUCKET, 'media.52poke.com'}/webp-cache/*"
environment:
BUCKET: ${opt:bucket, 'media.52poke.com'}
REGION: ${opt:region, 'ap-northeast-1'}
S3_BUCKET: ${env:S3_BUCKET, 'media.52poke.com'}
S3_REGION: ${env:S3_REGION, 'jp-osa-1'}
S3_ENDPOINT: ${env:S3_ENDPOINT, 'https://jp-osa-1.linodeobjects.com'}
S3_ACCESS_KEY_ID: ${env:S3_ACCESS_KEY_ID, ''}
S3_SECRET_ACCESS_KEY: ${env:S3_SECRET_ACCESS_KEY, ''}
apiGateway:
binaryMediaTypes:
- '*/*'
Expand Down
13 changes: 10 additions & 3 deletions src/utils/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import stream, { Readable } from 'stream';
import AWS, { S3, AWSError } from 'aws-sdk';
import createHttpError from 'http-errors';

AWS.config.region = process.env.REGION;
const s3 = new S3();
const bucket = process.env.BUCKET as string;
const s3 = new S3({
credentials: process.env.S3_ACCESS_KEY_ID && process.env.S3_SECRET_ACCESS_KEY ? {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
} : undefined,
endpoint: process.env.S3_ENDPOINT,
region: process.env.S3_REGION,
s3ForcePathStyle: true,
});
const bucket = process.env.S3_BUCKET as string;

export const existS3 = async (key: string): Promise<boolean> => {
try {
Expand Down

0 comments on commit d92a5aa

Please sign in to comment.