FIRStorageReference.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FIRStorage.h"
  18. #import "FIRStorageConstants.h"
  19. #import "FIRStorageDownloadTask.h"
  20. #import "FIRStorageListResult.h"
  21. #import "FIRStorageMetadata.h"
  22. #import "FIRStorageTask.h"
  23. #import "FIRStorageUploadTask.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. /**
  26. * FIRStorageReference represents a reference to a Google Cloud Storage object. Developers can
  27. * upload and download objects, as well as get/set object metadata, and delete an object at the
  28. * path.
  29. * @see https://cloud.google.com/storage/
  30. */
  31. NS_SWIFT_NAME(StorageReference)
  32. @interface FIRStorageReference : NSObject
  33. /**
  34. * The FIRStorage service object which created this reference.
  35. */
  36. @property(nonatomic, readonly) FIRStorage *storage;
  37. /**
  38. * The name of the Google Cloud Storage bucket associated with this reference,
  39. * in gs://bucket/path/to/object.txt, the bucket would be: 'bucket'
  40. */
  41. @property(nonatomic, readonly) NSString *bucket;
  42. /**
  43. * The full path to this object, not including the Google Cloud Storage bucket.
  44. * In gs://bucket/path/to/object.txt, the full path would be: 'path/to/object.txt'
  45. */
  46. @property(nonatomic, readonly) NSString *fullPath;
  47. /**
  48. * The short name of the object associated with this reference,
  49. * in gs://bucket/path/to/object.txt, the name of the object would be: 'object.txt'
  50. */
  51. @property(nonatomic, readonly) NSString *name;
  52. #pragma mark - Path Operations
  53. /**
  54. * Creates a new FIRStorageReference pointing to the root object.
  55. * @return A new FIRStorageReference pointing to the root object.
  56. */
  57. - (FIRStorageReference *)root;
  58. /**
  59. * Creates a new FIRStorageReference pointing to the parent of the current reference
  60. * or nil if this instance references the root location.
  61. * For example:
  62. * path = foo/bar/baz parent = foo/bar
  63. * path = foo parent = (root)
  64. * path = (root) parent = nil
  65. * @return A new FIRStorageReference pointing to the parent of the current reference.
  66. */
  67. - (nullable FIRStorageReference *)parent;
  68. /**
  69. * Creates a new FIRStorageReference pointing to a child object of the current reference.
  70. * path = foo child = bar newPath = foo/bar
  71. * path = foo/bar child = baz newPath = foo/bar/baz
  72. * All leading and trailing slashes will be removed, and consecutive slashes will be
  73. * compressed to single slashes. For example:
  74. * child = /foo/bar newPath = foo/bar
  75. * child = foo/bar/ newPath = foo/bar
  76. * child = foo///bar newPath = foo/bar
  77. * @param path Path to append to the current path.
  78. * @return A new FIRStorageReference pointing to a child location of the current reference.
  79. */
  80. - (FIRStorageReference *)child:(NSString *)path;
  81. #pragma mark - Uploads
  82. /**
  83. * Asynchronously uploads data to the currently specified FIRStorageReference,
  84. * without additional metadata.
  85. * This is not recommended for large files, and one should instead upload a file from disk.
  86. * @param uploadData The NSData to upload.
  87. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  88. */
  89. - (FIRStorageUploadTask *)putData:(NSData *)uploadData NS_SWIFT_NAME(putData(_:));
  90. /**
  91. * Asynchronously uploads data to the currently specified FIRStorageReference.
  92. * This is not recommended for large files, and one should instead upload a file from disk.
  93. * @param uploadData The NSData to upload.
  94. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  95. * about the object being uploaded.
  96. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  97. */
  98. // clang-format off
  99. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  100. metadata:(nullable FIRStorageMetadata *)metadata
  101. NS_SWIFT_NAME(putData(_:metadata:));
  102. // clang-format on
  103. /**
  104. * Asynchronously uploads data to the currently specified FIRStorageReference.
  105. * This is not recommended for large files, and one should instead upload a file from disk.
  106. * @param uploadData The NSData to upload.
  107. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  108. * about the object being uploaded.
  109. * @param completion A completion block that either returns the object metadata on success,
  110. * or an error on failure.
  111. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  112. */
  113. // clang-format off
  114. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  115. metadata:(nullable FIRStorageMetadata *)metadata
  116. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  117. NSError *_Nullable error))completion
  118. NS_SWIFT_NAME(putData(_:metadata:completion:));
  119. // clang-format on
  120. /**
  121. * Asynchronously uploads a file to the currently specified FIRStorageReference,
  122. * without additional metadata.
  123. * @param fileURL A URL representing the system file path of the object to be uploaded.
  124. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  125. */
  126. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL NS_SWIFT_NAME(putFile(from:));
  127. /**
  128. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  129. * @param fileURL A URL representing the system file path of the object to be uploaded.
  130. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  131. * about the object being uploaded.
  132. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  133. */
  134. // clang-format off
  135. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
  136. metadata:(nullable FIRStorageMetadata *)metadata
  137. NS_SWIFT_NAME(putFile(from:metadata:));
  138. // clang-format on
  139. /**
  140. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  141. * @param fileURL A URL representing the system file path of the object to be uploaded.
  142. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  143. * about the object being uploaded.
  144. * @param completion A completion block that either returns the object metadata on success,
  145. * or an error on failure.
  146. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  147. */
  148. // clang-format off
  149. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
  150. metadata:(nullable FIRStorageMetadata *)metadata
  151. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  152. NSError *_Nullable error))completion
  153. NS_SWIFT_NAME(putFile(from:metadata:completion:));
  154. // clang-format on
  155. #pragma mark - Downloads
  156. /**
  157. * Asynchronously downloads the object at the FIRStorageReference to an NSData object in memory.
  158. * An NSData of the provided max size will be allocated, so ensure that the device has enough free
  159. * memory to complete the download. For downloading large files, writeToFile may be a better option.
  160. * @param size The maximum size in bytes to download. If the download exceeds this size
  161. * the task will be cancelled and an error will be returned.
  162. * @param completion A completion block that either returns the object data on success,
  163. * or an error on failure.
  164. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  165. */
  166. // clang-format off
  167. - (FIRStorageDownloadTask *)dataWithMaxSize:(int64_t)size
  168. completion:(void (^)(NSData *_Nullable data,
  169. NSError *_Nullable error))completion
  170. NS_SWIFT_NAME(getData(maxSize:completion:));
  171. // clang-format on
  172. /**
  173. * Asynchronously retrieves a long lived download URL with a revokable token.
  174. * This can be used to share the file with others, but can be revoked by a developer
  175. * in the Firebase Console if desired.
  176. * @param completion A completion block that either returns the URL on success,
  177. * or an error on failure.
  178. */
  179. - (void)downloadURLWithCompletion:(void (^)(NSURL *_Nullable URL,
  180. NSError *_Nullable error))completion;
  181. /**
  182. * Asynchronously downloads the object at the current path to a specified system filepath.
  183. * @param fileURL A file system URL representing the path the object should be downloaded to.
  184. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  185. */
  186. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL;
  187. /**
  188. * Asynchronously downloads the object at the current path to a specified system filepath.
  189. * @param fileURL A file system URL representing the path the object should be downloaded to.
  190. * @param completion A completion block that fires when the file download completes.
  191. * Returns an NSURL pointing to the file path of the downloaded file on success,
  192. * or an error on failure.
  193. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  194. */
  195. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL
  196. completion:(nullable void (^)(NSURL *_Nullable URL,
  197. NSError *_Nullable error))completion;
  198. #pragma mark - List Support
  199. /**
  200. * List all items (files) and prefixes (folders) under this StorageReference.
  201. *
  202. * This is a helper method for calling list() repeatedly until there are no more results.
  203. * Consistency of the result is not guaranteed if objects are inserted or removed while this
  204. * operation is executing. All results are buffered in memory.
  205. *
  206. * `listAll(completion:)` is only available for projects using Firebase Rules Version 2.
  207. *
  208. * @param completion A completion handler that will be invoked with all items and prefixes under
  209. * the current StorageReference.
  210. */
  211. - (void)listAllWithCompletion:(void (^)(FIRStorageListResult *result,
  212. NSError *_Nullable error))completion;
  213. /**
  214. * List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  215. *
  216. * "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  217. * paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  218. * filtered.
  219. *
  220. * `list(maxResults:completion:)` is only available for projects using Firebase Rules Version 2.
  221. *
  222. * @param maxResults The maximum number of results to return in a single page. Must be greater
  223. * than 0 and at most 1000.
  224. * @param completion A completion handler that will be invoked with up to maxResults items and
  225. * prefixes under the current StorageReference.
  226. */
  227. - (void)listWithMaxResults:(int64_t)maxResults
  228. completion:
  229. (void (^)(FIRStorageListResult *result, NSError *_Nullable error))completion;
  230. /**
  231. * Resumes a previous call to list(maxResults:completion:)`, starting after a pagination token.
  232. * Returns the next set of items (files) and prefixes (folders) under this StorageReference.
  233. *
  234. * "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  235. * paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  236. * filtered.
  237. *
  238. * `list(maxResults:pageToken:completion:)`is only available for projects using Firebase Rules
  239. * Version 2.
  240. *
  241. * @param maxResults The maximum number of results to return in a single page. Must be greater
  242. * than 0 and at most 1000.
  243. * @param pageToken A page token from a previous call to list.
  244. * @param completion A completion handler that will be invoked with the next items and prefixes
  245. * under the current StorageReference.
  246. */
  247. - (void)listWithMaxResults:(int64_t)maxResults
  248. pageToken:(NSString *)pageToken
  249. completion:
  250. (void (^)(FIRStorageListResult *result, NSError *_Nullable error))completion;
  251. #pragma mark - Metadata Operations
  252. /**
  253. * Retrieves metadata associated with an object at the current path.
  254. * @param completion A completion block which returns the object metadata on success,
  255. * or an error on failure.
  256. */
  257. - (void)metadataWithCompletion:
  258. (void (^)(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error))completion
  259. NS_SWIFT_NAME(getMetadata(completion:));
  260. /**
  261. * Updates the metadata associated with an object at the current path.
  262. * @param metadata An FIRStorageMetadata object with the metadata to update.
  263. * @param completion A completion block which returns the FIRStorageMetadata on success,
  264. * or an error on failure.
  265. */
  266. // clang-format off
  267. - (void)updateMetadata:(FIRStorageMetadata *)metadata
  268. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  269. NSError *_Nullable error))completion
  270. NS_SWIFT_NAME(updateMetadata(_:completion:));
  271. // clang-format on
  272. #pragma mark - Delete
  273. /**
  274. * Deletes the object at the current path.
  275. * @param completion A completion block which returns nil on success, or an error on failure.
  276. */
  277. - (void)deleteWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  278. @end
  279. NS_ASSUME_NONNULL_END