-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathget-public-url.ts
63 lines (50 loc) · 2.13 KB
/
get-public-url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ACTOR_ENV_VARS, APIFY_ENV_VARS } from '@apify/consts';
import { createHmacSignature } from '@apify/utilities';
import { Args } from '@oclif/core';
import { getApifyStorageClient } from '../../lib/actor.js';
import { ApifyCommand } from '../../lib/apify_command.js';
import { CommandExitCodes } from '../../lib/consts.js';
import { error } from '../../lib/outputs.js';
export class GetPublicUrlCommand extends ApifyCommand<typeof GetPublicUrlCommand> {
static override description = 'Get an HTTP URL that allows public access to a key-value store item.';
static override args = {
key: Args.string({
required: true,
description: 'Key of the record in key-value store',
}),
};
async run() {
const { key } = this.args;
if ([undefined, 'false', ''].includes(process.env[APIFY_ENV_VARS.IS_AT_HOME])) {
error({ message: 'get-public-url is not yet implemented for local development' });
process.exitCode = CommandExitCodes.NotImplemented;
return;
}
const apiBase = process.env[APIFY_ENV_VARS.API_PUBLIC_BASE_URL];
const storeId = process.env[ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID];
// This should never happen, but handle it gracefully to prevent crashes.
if (!storeId) {
error({
message: `Missing environment variable: ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}. Please set it before running the command.`,
});
process.exitCode = CommandExitCodes.InvalidInput;
return;
}
const apifyClient = await getApifyStorageClient();
const store = await apifyClient.keyValueStore(storeId).get();
const publicUrl = new URL(`${apiBase}/v2/key-value-stores/${storeId}/records/${key}`);
if (!store) {
error({
message: `Key-Value store with ID '${storeId}' was not found. Ensure the store exists and that the correct ID is set in ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}.`,
});
process.exitCode = CommandExitCodes.NotFound;
return;
}
// @ts-expect-error Add types to client
const { urlSigningSecretKey } = store;
if (urlSigningSecretKey) {
publicUrl.searchParams.append('signature', createHmacSignature(urlSigningSecretKey as string, key));
}
console.log(publicUrl.toString());
}
}