English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE คนไทย Français русский

回答

收藏

Telegram 小程序 | TypeScript @telegram-apps/sdk @2.x | 组件 | 云存储

开源社区 开源社区 8598 人阅读 | 0 人回复 | 2025-02-24

云存储

负责管理 Telegram 迷你应用程序中云存储的💠组件

检查支持

要检查当前 Telegram 迷你应用程序版本是否支持云存储,需要使用 isSupported 方法:

Variable :

  1. import { cloudStorage } from '@telegram-apps/sdk';) o, H7 q7 B1 Y6 {! B& N. i5 c

  2. - t# {0 c4 n8 C* z$ T  f3 e
  3. cloudStorage.isSupported(); // boolean
复制代码
7 I( a- C9 C6 @

Functions :

  1. import { isCloudStorageSupported } from '@telegram-apps/sdk';
    + r( H6 ~1 {, \6 ^
  2. : [2 G* E/ y5 J  Z* a$ @
  3. isCloudStorageSupported(); // boolean
复制代码
设置项目

要设置键值,请使用 setItem 方法。

Variable :

  1. await cloudStorage.setItem('a', 'a-value');
复制代码

Functions :

  1. import { setCloudStorageItem } from '@telegram-apps/sdk';( v! ~( c5 I( ?( w5 \; Q2 F$ G; f
  2. : o3 d* h- f) X
  3. await setCloudStorageItem('a', 'a-value');
复制代码
获取钥匙

要获取所有现有密钥的列表,请使用 getKeys 方法。

Variable :

  1. const keys = await cloudStorage.getKeys(); // ['a', 'b', 'c']
复制代码

Functions :

  1. import { getCloudStorageKeys } from '@telegram-apps/sdk';
    / \, @! ^* e. q. A9 R5 l
  2. " [6 S/ E1 {) S9 q2 M
  3. const keys = await getCloudStorageKeys(); // ['a', 'b', 'c']
复制代码

7 _) Z( |% I) ?+ s3 p

要获取特定键或多个键的值,请使用 getItem 方法。

Variable :

  1. const nonExistent = await cloudStorage.getItem('non-existent');- y/ p1 |; }6 t/ V
  2. // The result is an empty string: ''* z2 g4 }* M3 R* J7 C+ a: S

  3. , L# O5 e& S* w# Z1 x4 B
  4. const existent = await cloudStorage.getItem('a');& |' P* e  q, N! O6 F
  5. // The result is the value of the 'a' key. Example: 'a-value'" {! l0 m: ?9 q8 p& u( ^/ Q5 V
  6. # p! w  }% h2 \6 s, K/ D  W/ B4 v; j; ^
  7. const values = await cloudStorage.getItem(['a', 'b', 'non-existent']);# v: D" N5 V& i  f% F9 E8 T* N
  8. // The result is a record of the keys 'a', 'b', and 'non-existent'. ) f$ b  q' p) V
  9. // Example:
    % m9 u8 S0 G2 ~1 M" B
  10. // { 5 c& Z1 ~  D( B2 b4 h2 D) [
  11. //   a: 'a-value', 4 W; _! S' c2 J2 \
  12. //   b: 'b-value', " P' T% X) C7 \+ U& L* A0 m# a! r
  13. //   'non-existent': '', - U0 a. Y6 Z3 y* y
  14. // }
复制代码

Functions :

  1. import { getCloudStorageItem } from '@telegram-apps/sdk';
    $ m$ `% Q$ H6 {% R4 C9 E! F' |

  2. . i8 \- p) Q& Z- B
  3. const nonExistent = await getCloudStorageItem('non-existent');
    & R1 ~! ~3 r9 l1 L+ W' l
  4. // The result is an empty string: ''! ]: w9 p/ l* F- q2 \: E
  5.   Q) k' J, f" ~- N4 p& `( B
  6. const existent = await getCloudStorageItem('a');
    / H! I9 a9 x" N/ [  N9 F( L- U# Z2 b
  7. // The result is the value of the 'a' key. Example: 'a-value'. b5 [# b0 f  r4 d

  8. ! t* t  N& J% \& \
  9. const values = await getCloudStorageItem(['a', 'b', 'non-existent']);
    . p" q- u& M6 \
  10. // The result is a record of the keys 'a', 'b', and 'non-existent'.
    6 E0 A3 \  w, Z; a7 J5 p
  11. // Example:
    2 y" i+ Z) L, Q( N9 `% d
  12. // { / w( X8 y, _- L3 n! W0 t2 v
  13. //   a: 'a-value', : ?0 ^! I: A5 L1 {( F& }0 V
  14. //   b: 'b-value',
    # Z2 a' F" t6 k3 e$ f$ k/ k3 c$ ~; d. ]
  15. //   'non-existent': '', : @9 |6 U/ K6 k. O6 F4 F' g
  16. // }
复制代码
删除项目

要删除一个键或键列表,请使用 deleteItem 方法。

Variable :

  1. await cloudStorage.deleteItem('a');3 {* ]+ H7 ^* @$ W# v6 L- K! m: T
  2. await cloudStorage.deleteItem(['a', 'b', 'c']);
复制代码

Functions :

  1. import { deleteCloudStorageItem } from '@telegram-apps/sdk';
    ; Y8 H" V6 V$ x2 I6 d& S* o1 ]

  2. # K; v6 n+ d" K' @4 Y
  3. await deleteCloudStorageItem('a');( _0 J' B6 ?2 a8 K. w7 w% p
  4. await deleteCloudStorageItem(['a', 'b', 'c']);
复制代码
+ V0 a  D- C! G+ A$ I+ U4 z( |% y
% l+ [3 \6 f, u/ _! A3 f0 L2 _
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则