-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage-example.js
More file actions
182 lines (155 loc) · 5.38 KB
/
Copy pathstorage-example.js
File metadata and controls
182 lines (155 loc) · 5.38 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Example: Demonstrating how to use the Scrapeless SDK storage module
* Filename: storage-example.mjs
*/
import { ScrapelessClient } from '@scrapeless-ai/sdk';
/**
* Demonstrates how to create and use the dataset storage
*/
async function datasetExample(client) {
try {
console.log('Dataset example:');
// Create a new dataset
const dataset = await client.storage.dataset.createDataset('products-data');
console.log(`Dataset created with ID: ${dataset.id}`);
// Add items to the dataset
await client.storage.dataset.addItems(dataset.id, [
{ name: 'Product 1', price: 19.99, category: 'Electronics' },
{ name: 'Product 2', price: 29.99, category: 'Home' },
{ name: 'Product 3', price: 9.99, category: 'Clothing' }
]);
console.log('Items added to dataset');
// Get items from the dataset
const items = await client.storage.dataset.getItems(dataset.id, {
page: 1,
pageSize: 10
});
console.log('Dataset items:', items);
console.log('Dataset example completed\n');
} catch (error) {
console.error('Dataset example error:', error);
}
}
/**
* Demonstrates how to create and use the key-value storage
*/
async function kvStorageExample(client) {
try {
console.log('Key-Value storage example:');
// Create a new KV namespace
const namespace = await client.storage.kv.createNamespace('config-store');
console.log(`KV namespace created with ID: ${namespace.id}`);
// Set values in the namespace
await client.storage.kv.setValue(namespace.id, {
key: 'appConfig',
value: JSON.stringify({
apiVersion: '1.0',
features: {
darkMode: true,
notifications: true
}
})
});
console.log('Value set in KV store');
// Get value from the namespace
const config = await client.storage.kv.getValue(namespace.id, 'appConfig');
console.log('Retrieved config:', JSON.parse(config));
// List keys in the namespace
const keys = await client.storage.kv.listKeys(namespace.id, { page: 1, pageSize: 10 });
console.log('KV store keys:', keys);
console.log('KV storage example completed\n');
} catch (error) {
console.error('KV storage example error:', error);
}
}
/**
* Demonstrates how to create and use the object storage
*/
async function objectStorageExample(client) {
try {
console.log('Object storage example:');
// Create a new bucket
const bucket = await client.storage.object.createBucket({
name: 'images-bucket',
description: 'Storage for product images'
});
console.log(`Object bucket created with ID: ${bucket.id}`);
// Upload a file to the bucket (in a real scenario, you would use a real file path)
const uploadResult = await client.storage.object.put(bucket.id, {
file: 'example/sample-image.jpg'
});
console.log('File uploaded to object storage:', uploadResult);
// List objects in the bucket
const objects = await client.storage.object.list(bucket.id, { page: 1, pageSize: 10 });
console.log('Objects in bucket:', objects);
console.log('Object storage example completed\n');
} catch (error) {
console.error('Object storage example error:', error);
}
}
/**
* Demonstrates how to create and use the queue storage
*/
async function queueStorageExample(client) {
try {
console.log('Queue storage example:');
// Create a new queue
const queue = await client.storage.queue.create({
name: 'processing-tasks',
description: 'Queue for processing tasks'
});
console.log(`Queue created with ID: ${queue.id}`);
// Push messages to the queue
const message1 = await client.storage.queue.push(queue.id, {
name: 'processImage',
payload: JSON.stringify({
imageId: '123',
effects: ['resize', 'grayscale']
}),
retry: 3,
timeout: 300,
deadline: Math.floor(Date.now() / 1000) + 3600 // 1 hour from now
});
console.log('Message pushed to queue:', message1);
const message2 = await client.storage.queue.push(queue.id, {
name: 'generateReport',
payload: JSON.stringify({ reportType: 'monthly', format: 'pdf' }),
retry: 2,
timeout: 600,
deadline: Math.floor(Date.now() / 1000) + 7200 // 2 hours from now
});
console.log('Message pushed to queue:', message2);
// Pull messages from the queue
const messages = await client.storage.queue.pull(queue.id);
console.log('Messages pulled from queue:', messages);
// Acknowledge a message (if any)
if (messages && messages.length > 0) {
await client.storage.queue.ack(queue.id, messages[0].id);
console.log(`Message ${messages[0].id} acknowledged`);
}
console.log('Queue storage example completed\n');
} catch (error) {
console.error('Queue storage example error:', error);
}
}
/**
* Main example function
*/
async function runExample() {
try {
// Initialize the Scrapeless client
const client = new ScrapelessClient({
apiKey: process.env.SCRAPELESS_API_KEY || 'your_api_key_here'
});
// Run the examples
await datasetExample(client);
await kvStorageExample(client);
// await objectStorageExample(client);
await queueStorageExample(client);
console.log('All storage examples completed successfully');
} catch (error) {
console.error('Example error:', error);
}
}
// Run the example
runExample().catch(console.error);