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

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

回答

收藏

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

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

云存储

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

检查支持

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

Variable :

  1. import { cloudStorage } from '@telegram-apps/sdk';& J2 z1 |1 R9 K7 u/ F
  2. 0 J. I: @. ?; ~; s: c
  3. cloudStorage.isSupported(); // boolean
复制代码

' o) Y3 b, ]' C4 w

Functions :

  1. import { isCloudStorageSupported } from '@telegram-apps/sdk';, M% _; y+ {8 F/ k/ M# D

  2. , t; l: J& k) k4 R; g7 L
  3. isCloudStorageSupported(); // boolean
复制代码
设置项目

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

Variable :

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

Functions :

  1. import { setCloudStorageItem } from '@telegram-apps/sdk';
    ) ^* T. H5 Q7 u$ |/ [$ K

  2. 6 |! E0 x8 X: d" ]# L" J; E7 P8 T
  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';8 H" |( i; x+ w# |! J( v
  2. ( a  B4 t* [) `  q5 y0 {% M. z7 t
  3. const keys = await getCloudStorageKeys(); // ['a', 'b', 'c']
复制代码

9 c2 m% K/ R7 v! l* [% x

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

Variable :

  1. const nonExistent = await cloudStorage.getItem('non-existent');
    . W) b* R# n3 _: m/ M1 y$ O
  2. // The result is an empty string: ''
    1 u3 k# \2 i( y# V! d1 _
  3. # `( R3 f/ }; {" q! x
  4. const existent = await cloudStorage.getItem('a');
    0 ~: z# ~( D  B, o5 Z' A8 f. K
  5. // The result is the value of the 'a' key. Example: 'a-value'* ^* N1 i0 y$ f
  6. ( A; K2 i% R3 u9 {* D( h! m4 f
  7. const values = await cloudStorage.getItem(['a', 'b', 'non-existent']);3 P  a% ]' M5 g
  8. // The result is a record of the keys 'a', 'b', and 'non-existent'. ' g, {" W( D$ w+ m2 B: Z
  9. // Example:
    1 ?/ }1 F, M$ \5 j
  10. // {
    ! s: h  U4 P9 K
  11. //   a: 'a-value',
      w+ q" p9 ?8 ]) L: X6 V
  12. //   b: 'b-value',
    1 Y& C, d+ |1 p) }# p  X7 r" P
  13. //   'non-existent': '', ' j$ }& ?/ @4 L% C$ f9 H- f# B- @/ T5 k
  14. // }
复制代码

Functions :

  1. import { getCloudStorageItem } from '@telegram-apps/sdk';- S2 _1 f% u; |8 t0 T( j

  2. 0 K4 t9 g2 \! J6 L6 u
  3. const nonExistent = await getCloudStorageItem('non-existent');
    $ s1 [: O+ @8 O. i3 J& Q6 i
  4. // The result is an empty string: ''0 T  i3 S: ?9 r! Z: l7 W  y  _

  5. " u8 p  Z  V0 ?- v4 ?
  6. const existent = await getCloudStorageItem('a');8 ~+ m3 @( `) f) I) _
  7. // The result is the value of the 'a' key. Example: 'a-value'
    3 Q# v5 G: Z: E) P" [

  8. : K! V: o5 C7 f
  9. const values = await getCloudStorageItem(['a', 'b', 'non-existent']);0 ~( O5 _9 i# A. W
  10. // The result is a record of the keys 'a', 'b', and 'non-existent'. * K; L  W. X9 I" w; T6 K7 J3 N6 M
  11. // Example:
    : @7 E. j1 X2 B$ z- I$ w
  12. // {
    3 G7 |, |( T; Z  G2 E
  13. //   a: 'a-value', # s5 x9 L; b& \% J/ ]
  14. //   b: 'b-value', ! [+ [1 j; }  a7 y, T8 Q$ q
  15. //   'non-existent': '',
    + x# J) ~3 z# i  v, V0 ^
  16. // }
复制代码
删除项目

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

Variable :

  1. await cloudStorage.deleteItem('a');9 [2 p( P" u! o/ I" }( G
  2. await cloudStorage.deleteItem(['a', 'b', 'c']);
复制代码

Functions :

  1. import { deleteCloudStorageItem } from '@telegram-apps/sdk';  K4 e3 T' O# C, U0 c

  2. 8 O+ U# Q0 O6 C% k3 X: H+ F: L8 q! H
  3. await deleteCloudStorageItem('a');
    3 c+ K( S( r7 `0 A8 B8 }
  4. await deleteCloudStorageItem(['a', 'b', 'c']);
复制代码
- ]; G9 W( f4 ~  t

% n8 H) \7 q* f, n
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则