fix: enable non-access-key flows

This commit is contained in:
Alexander Nicholson 4584443+DragonStuff@users.noreply.github.com
2024-11-20 18:37:13 +09:00
parent 62562c9af6
commit 4d22d93e90
3 changed files with 19 additions and 19 deletions

View File

@@ -5,12 +5,10 @@ import { objectList } from "../templates/components/object_list.ts";
const apiRoutes = new Hono(); const apiRoutes = new Hono();
const storageService = new StorageService( const storageService = new StorageService({
Deno.env.get("S3_BUCKET") || "", bucket: Deno.env.get("S3_BUCKET") || "",
Deno.env.get("S3_REGION") || "", region: Deno.env.get("S3_REGION") || ""
Deno.env.get("AWS_ACCESS_KEY_ID") || "", });
Deno.env.get("AWS_SECRET_ACCESS_KEY") || "",
);
const searchService = new SearchService(storageService); const searchService = new SearchService(storageService);

View File

@@ -4,16 +4,13 @@ import { browser } from "../templates/browser.ts";
import { layout } from "../templates/layout.ts"; import { layout } from "../templates/layout.ts";
import { objectList } from "../templates/components/object_list.ts"; import { objectList } from "../templates/components/object_list.ts";
import { pagination } from "../templates/components/pagination.ts"; import { pagination } from "../templates/components/pagination.ts";
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.48/deno-dom-wasm.ts";
const viewRoutes = new Hono(); const viewRoutes = new Hono();
const storageService = new StorageService( const storageService = new StorageService({
Deno.env.get("S3_BUCKET") || "", bucket: Deno.env.get("S3_BUCKET") || "",
Deno.env.get("S3_REGION") || "", region: Deno.env.get("S3_REGION") || ""
Deno.env.get("AWS_ACCESS_KEY_ID") || "", });
Deno.env.get("AWS_SECRET_ACCESS_KEY") || "",
);
viewRoutes.get("/", async (c) => { viewRoutes.get("/", async (c) => {
const prefix = c.req.query("prefix") || ""; const prefix = c.req.query("prefix") || "";

View File

@@ -10,18 +10,23 @@ interface Credentials {
sessionToken?: string; sessionToken?: string;
} }
interface StorageServiceConfig {
bucket: string;
region: string;
}
export class StorageService { export class StorageService {
private client: S3; private client: S3;
private credentials!: Credentials; private credentials!: Credentials;
constructor(private bucket: string, private region: string) { constructor(private config: StorageServiceConfig) {
this.initializeClient(); this.initializeClient();
} }
private async initializeClient() { private async initializeClient() {
this.credentials = await this.resolveCredentials(); this.credentials = await this.resolveCredentials();
const factory = new ApiFactory({ const factory = new ApiFactory({
region: this.region, region: this.config.region,
credentials: this.credentials, credentials: this.credentials,
}); });
@@ -114,9 +119,9 @@ export class StorageService {
return await getSignedUrl({ return await getSignedUrl({
accessKeyId: this.credentials.awsAccessKeyId, accessKeyId: this.credentials.awsAccessKeyId,
secretAccessKey: this.credentials.awsSecretKey, secretAccessKey: this.credentials.awsSecretKey,
bucket: this.bucket, bucket: this.config.bucket,
key, key,
region: this.region, region: this.config.region,
expiresIn, expiresIn,
}); });
} }
@@ -125,7 +130,7 @@ export class StorageService {
await this.ensureInitialized(); await this.ensureInitialized();
try { try {
const params: ListObjectsV2Request = { const params: ListObjectsV2Request = {
Bucket: this.bucket, Bucket: this.config.bucket,
Prefix: options.prefix, Prefix: options.prefix,
Delimiter: options.delimiter, Delimiter: options.delimiter,
MaxKeys: options.maxKeys, MaxKeys: options.maxKeys,
@@ -163,7 +168,7 @@ export class StorageService {
private async getContentType(key: string): Promise<string | undefined> { private async getContentType(key: string): Promise<string | undefined> {
try { try {
const response = await this.client.headObject({ const response = await this.client.headObject({
Bucket: this.bucket, Bucket: this.config.bucket,
Key: key, Key: key,
}); });
return response.ContentType; return response.ContentType;