云存储 负责管理 Telegram 迷你应用程序中云存储的💠组件。 检查支持要检查当前 Telegram 迷你应用程序版本是否支持云存储,需要使用 isSupported 方法: Variable : - import { cloudStorage } from '@telegram-apps/sdk';) o, H7 q7 B1 Y6 {! B& N. i5 c
- t# {0 c4 n8 C* z$ T f3 e- cloudStorage.isSupported(); // boolean
复制代码 7 I( a- C9 C6 @
Functions : - import { isCloudStorageSupported } from '@telegram-apps/sdk';
+ r( H6 ~1 {, \6 ^ - : [2 G* E/ y5 J Z* a$ @
- isCloudStorageSupported(); // boolean
复制代码 设置项目要设置键值,请使用 setItem 方法。 Variable : - await cloudStorage.setItem('a', 'a-value');
复制代码Functions : - import { setCloudStorageItem } from '@telegram-apps/sdk';( v! ~( c5 I( ?( w5 \; Q2 F$ G; f
- : o3 d* h- f) X
- await setCloudStorageItem('a', 'a-value');
复制代码 获取钥匙要获取所有现有密钥的列表,请使用 getKeys 方法。 Variable : - const keys = await cloudStorage.getKeys(); // ['a', 'b', 'c']
复制代码Functions : - import { getCloudStorageKeys } from '@telegram-apps/sdk';
/ \, @! ^* e. q. A9 R5 l - " [6 S/ E1 {) S9 q2 M
- const keys = await getCloudStorageKeys(); // ['a', 'b', 'c']
复制代码
7 _) Z( |% I) ?+ s3 p要获取特定键或多个键的值,请使用 getItem 方法。 Variable : - const nonExistent = await cloudStorage.getItem('non-existent');- y/ p1 |; }6 t/ V
- // The result is an empty string: ''* z2 g4 }* M3 R* J7 C+ a: S
, L# O5 e& S* w# Z1 x4 B- const existent = await cloudStorage.getItem('a');& |' P* e q, N! O6 F
- // The result is the value of the 'a' key. Example: 'a-value'" {! l0 m: ?9 q8 p& u( ^/ Q5 V
- # p! w }% h2 \6 s, K/ D W/ B4 v; j; ^
- const values = await cloudStorage.getItem(['a', 'b', 'non-existent']);# v: D" N5 V& i f% F9 E8 T* N
- // The result is a record of the keys 'a', 'b', and 'non-existent'. ) f$ b q' p) V
- // Example:
% m9 u8 S0 G2 ~1 M" B - // { 5 c& Z1 ~ D( B2 b4 h2 D) [
- // a: 'a-value', 4 W; _! S' c2 J2 \
- // b: 'b-value', " P' T% X) C7 \+ U& L* A0 m# a! r
- // 'non-existent': '', - U0 a. Y6 Z3 y* y
- // }
复制代码Functions : - import { getCloudStorageItem } from '@telegram-apps/sdk';
$ m$ `% Q$ H6 {% R4 C9 E! F' |
. i8 \- p) Q& Z- B- const nonExistent = await getCloudStorageItem('non-existent');
& R1 ~! ~3 r9 l1 L+ W' l - // The result is an empty string: ''! ]: w9 p/ l* F- q2 \: E
- Q) k' J, f" ~- N4 p& `( B
- const existent = await getCloudStorageItem('a');
/ H! I9 a9 x" N/ [ N9 F( L- U# Z2 b - // The result is the value of the 'a' key. Example: 'a-value'. b5 [# b0 f r4 d
! t* t N& J% \& \- const values = await getCloudStorageItem(['a', 'b', 'non-existent']);
. p" q- u& M6 \ - // The result is a record of the keys 'a', 'b', and 'non-existent'.
6 E0 A3 \ w, Z; a7 J5 p - // Example:
2 y" i+ Z) L, Q( N9 `% d - // { / w( X8 y, _- L3 n! W0 t2 v
- // a: 'a-value', : ?0 ^! I: A5 L1 {( F& }0 V
- // b: 'b-value',
# Z2 a' F" t6 k3 e$ f$ k/ k3 c$ ~; d. ] - // 'non-existent': '', : @9 |6 U/ K6 k. O6 F4 F' g
- // }
复制代码 删除项目要删除一个键或键列表,请使用 deleteItem 方法。 Variable : - await cloudStorage.deleteItem('a');3 {* ]+ H7 ^* @$ W# v6 L- K! m: T
- await cloudStorage.deleteItem(['a', 'b', 'c']);
复制代码Functions : - import { deleteCloudStorageItem } from '@telegram-apps/sdk';
; Y8 H" V6 V$ x2 I6 d& S* o1 ]
# K; v6 n+ d" K' @4 Y- await deleteCloudStorageItem('a');( _0 J' B6 ?2 a8 K. w7 w% p
- await deleteCloudStorageItem(['a', 'b', 'c']);
复制代码 + V0 a D- C! G+ A$ I+ U4 z( |% y
% l+ [3 \6 f, u/ _! A3 f0 L2 _
|