add: delete actions for device + slightly better ui
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
DeleteObjectCommand,
|
||||
GetObjectCommand,
|
||||
HeadObjectCommand,
|
||||
ListObjectsV2Command,
|
||||
PutObjectCommand,
|
||||
S3Client,
|
||||
} from "@aws-sdk/client-s3";
|
||||
@@ -481,4 +482,51 @@ export class R2StorageClient {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List object keys in R2 bucket, optionally filtered by prefix
|
||||
*/
|
||||
async listObjectKeys(prefix?: string): Promise<Result<string[]>> {
|
||||
try {
|
||||
const keys: string[] = [];
|
||||
let continuationToken: string | undefined = undefined;
|
||||
|
||||
do {
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: this.config.bucketName,
|
||||
Prefix: prefix,
|
||||
ContinuationToken: continuationToken,
|
||||
});
|
||||
|
||||
const response = await this.s3Client.send(command);
|
||||
const pageKeys =
|
||||
response.Contents?.map((item) => item.Key).filter(
|
||||
(key): key is string => Boolean(key),
|
||||
) || [];
|
||||
keys.push(...pageKeys);
|
||||
|
||||
continuationToken = response.IsTruncated
|
||||
? response.NextContinuationToken
|
||||
: undefined;
|
||||
} while (continuationToken);
|
||||
|
||||
logger.info(
|
||||
`Listed ${keys.length} objects${prefix ? ` with prefix ${prefix}` : ""}`,
|
||||
);
|
||||
return { data: keys };
|
||||
} catch (error) {
|
||||
logger.error("Failed to list object keys:", error);
|
||||
return {
|
||||
error: getError(
|
||||
{
|
||||
code: ERROR_CODES.STORAGE_ERROR,
|
||||
message: "Failed to list objects",
|
||||
description: "Could not list objects in storage",
|
||||
detail: "S3 list operation failed",
|
||||
},
|
||||
error,
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user