Skip to content

Commit

Permalink
chore: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Dec 25, 2024
1 parent aed6fe3 commit f982538
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/save-file-for-ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import path from 'path';
import dotenv from 'dotenv';

import { Session } from '@yandex-cloud/nodejs-sdk/dist/session';

import { initFileSdk } from '@yandex-cloud/nodejs-sdk/ai-files-v1/sdk';
import { ExpirationConfig_ExpirationPolicy } from '@yandex-cloud/nodejs-sdk/ai-files-v1/generated/yandex/cloud/ai/common/common';

import { readFile } from 'fs/promises';

dotenv.config({ path: path.resolve(__dirname, '.env') });

const getEnv = (envName: string, defaultValue?: string): string => {
const envValue = process.env[envName] || defaultValue;

if (!envValue) {
throw new Error(`Env variable ${envName} is not defined`);
}

return envValue;
};

const iamToken = getEnv('YC_IAM_TOKEN');
const folderId = getEnv('YC_FOLDER_ID');

(async function () {
const session = new Session({ iamToken });
const fileSdk = initFileSdk(session);

const fileToSaveContent = await readFile('./SomeFileToSave.pdf', {
encoding: 'binary',
});

const content = Buffer.from(fileToSaveContent, 'binary');

const file = await fileSdk.create({
folderId,
expirationConfig: {
ttlDays: 2,
expirationPolicy: ExpirationConfig_ExpirationPolicy.STATIC,
},
mimeType: 'application/pdf',
content,
});

console.log(file);

const r = await fileSdk.getUrl({ fileId: file.id });
console.log(r);
})();

0 comments on commit f982538

Please sign in to comment.