云存储 负责管理 Telegram 迷你应用程序中云存储的💠组件。 检查支持要检查当前 Telegram 迷你应用程序版本是否支持云存储,需要使用 isSupported 方法: Variable : - import { cloudStorage } from '@telegram-apps/sdk';& J2 z1 |1 R9 K7 u/ F
- 0 J. I: @. ?; ~; s: c
- cloudStorage.isSupported(); // boolean
复制代码
' o) Y3 b, ]' C4 wFunctions : - import { isCloudStorageSupported } from '@telegram-apps/sdk';, M% _; y+ {8 F/ k/ M# D
, t; l: J& k) k4 R; g7 L- isCloudStorageSupported(); // boolean
复制代码 设置项目要设置键值,请使用 setItem 方法。 Variable : - await cloudStorage.setItem('a', 'a-value');
复制代码Functions : - import { setCloudStorageItem } from '@telegram-apps/sdk';
) ^* T. H5 Q7 u$ |/ [$ K
6 |! E0 x8 X: d" ]# L" J; E7 P8 T- await setCloudStorageItem('a', 'a-value');
复制代码 获取钥匙要获取所有现有密钥的列表,请使用 getKeys 方法。 Variable : - const keys = await cloudStorage.getKeys(); // ['a', 'b', 'c']
复制代码Functions : - import { getCloudStorageKeys } from '@telegram-apps/sdk';8 H" |( i; x+ w# |! J( v
- ( a B4 t* [) ` q5 y0 {% M. z7 t
- const keys = await getCloudStorageKeys(); // ['a', 'b', 'c']
复制代码
9 c2 m% K/ R7 v! l* [% x要获取特定键或多个键的值,请使用 getItem 方法。 Variable : - const nonExistent = await cloudStorage.getItem('non-existent');
. W) b* R# n3 _: m/ M1 y$ O - // The result is an empty string: ''
1 u3 k# \2 i( y# V! d1 _ - # `( R3 f/ }; {" q! x
- const existent = await cloudStorage.getItem('a');
0 ~: z# ~( D B, o5 Z' A8 f. K - // The result is the value of the 'a' key. Example: 'a-value'* ^* N1 i0 y$ f
- ( A; K2 i% R3 u9 {* D( h! m4 f
- const values = await cloudStorage.getItem(['a', 'b', 'non-existent']);3 P a% ]' M5 g
- // The result is a record of the keys 'a', 'b', and 'non-existent'. ' g, {" W( D$ w+ m2 B: Z
- // Example:
1 ?/ }1 F, M$ \5 j - // {
! s: h U4 P9 K - // a: 'a-value',
w+ q" p9 ?8 ]) L: X6 V - // b: 'b-value',
1 Y& C, d+ |1 p) }# p X7 r" P - // 'non-existent': '', ' j$ }& ?/ @4 L% C$ f9 H- f# B- @/ T5 k
- // }
复制代码Functions : - import { getCloudStorageItem } from '@telegram-apps/sdk';- S2 _1 f% u; |8 t0 T( j
0 K4 t9 g2 \! J6 L6 u- const nonExistent = await getCloudStorageItem('non-existent');
$ s1 [: O+ @8 O. i3 J& Q6 i - // The result is an empty string: ''0 T i3 S: ?9 r! Z: l7 W y _
" u8 p Z V0 ?- v4 ?- const existent = await getCloudStorageItem('a');8 ~+ m3 @( `) f) I) _
- // The result is the value of the 'a' key. Example: 'a-value'
3 Q# v5 G: Z: E) P" [
: K! V: o5 C7 f- const values = await getCloudStorageItem(['a', 'b', 'non-existent']);0 ~( O5 _9 i# A. W
- // The result is a record of the keys 'a', 'b', and 'non-existent'. * K; L W. X9 I" w; T6 K7 J3 N6 M
- // Example:
: @7 E. j1 X2 B$ z- I$ w - // {
3 G7 |, |( T; Z G2 E - // a: 'a-value', # s5 x9 L; b& \% J/ ]
- // b: 'b-value', ! [+ [1 j; } a7 y, T8 Q$ q
- // 'non-existent': '',
+ x# J) ~3 z# i v, V0 ^ - // }
复制代码 删除项目要删除一个键或键列表,请使用 deleteItem 方法。 Variable : - await cloudStorage.deleteItem('a');9 [2 p( P" u! o/ I" }( G
- await cloudStorage.deleteItem(['a', 'b', 'c']);
复制代码Functions : - import { deleteCloudStorageItem } from '@telegram-apps/sdk'; K4 e3 T' O# C, U0 c
8 O+ U# Q0 O6 C% k3 X: H+ F: L8 q! H- await deleteCloudStorageItem('a');
3 c+ K( S( r7 `0 A8 B8 } - await deleteCloudStorageItem(['a', 'b', 'c']);
复制代码 - ]; G9 W( f4 ~ t
% n8 H) \7 q* f, n |