FIRStorageReference.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "FIRStorageMetadata.h"
  21. #import "FIRStorageTask.h"
  22. #import "FIRStorageUploadTask.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. /**
  25. * FIRStorageReference represents a reference to a Google Cloud Storage object. Developers can
  26. * upload and download objects, as well as get/set object metadata, and delete an object at the
  27. * path.
  28. * @see https://cloud.google.com/storage/
  29. */
  30. NS_SWIFT_NAME(StorageReference)
  31. @interface FIRStorageReference : NSObject
  32. /**
  33. * The FIRStorage service object which created this reference.
  34. */
  35. @property(nonatomic, readonly) FIRStorage *storage;
  36. /**
  37. * The name of the Google Cloud Storage bucket associated with this reference,
  38. * in gs://bucket/path/to/object.txt, the bucket would be: 'bucket'
  39. */
  40. @property(nonatomic, readonly) NSString *bucket;
  41. /**
  42. * The full path to this object, not including the Google Cloud Storage bucket.
  43. * In gs://bucket/path/to/object.txt, the full path would be: 'path/to/object.txt'
  44. */
  45. @property(nonatomic, readonly) NSString *fullPath;
  46. /**
  47. * The short name of the object associated with this reference,
  48. * in gs://bucket/path/to/object.txt, the name of the object would be: 'object.txt'
  49. */
  50. @property(nonatomic, readonly) NSString *name;
  51. #pragma mark - Path Operations
  52. /**
  53. * Creates a new FIRStorageReference pointing to the root object.
  54. * @return A new FIRStorageReference pointing to the root object.
  55. */
  56. - (FIRStorageReference *)root;
  57. /**
  58. * Creates a new FIRStorageReference pointing to the parent of the current reference
  59. * or nil if this instance references the root location.
  60. * For example:
  61. * path = foo/bar/baz parent = foo/bar
  62. * path = foo parent = (root)
  63. * path = (root) parent = nil
  64. * @return A new FIRStorageReference pointing to the parent of the current reference.
  65. */
  66. - (nullable FIRStorageReference *)parent;
  67. /**
  68. * Creates a new FIRStorageReference pointing to a child object of the current reference.
  69. * path = foo child = bar newPath = foo/bar
  70. * path = foo/bar child = baz newPath = foo/bar/baz
  71. * All leading and trailing slashes will be removed, and consecutive slashes will be
  72. * compressed to single slashes. For example:
  73. * child = /foo/bar newPath = foo/bar
  74. * child = foo/bar/ newPath = foo/bar
  75. * child = foo///bar newPath = foo/bar
  76. * @param path Path to append to the current path.
  77. * @return A new FIRStorageReference pointing to a child location of the current reference.
  78. */
  79. - (FIRStorageReference *)child:(NSString *)path;
  80. #pragma mark - Uploads
  81. /**
  82. * Asynchronously uploads data to the currently specified FIRStorageReference,
  83. * without additional metadata.
  84. * This is not recommended for large files, and one should instead upload a file from disk.
  85. * @param uploadData The NSData to upload.
  86. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  87. */
  88. - (FIRStorageUploadTask *)putData:(NSData *)uploadData NS_SWIFT_NAME(putData(_:));
  89. /**
  90. * Asynchronously uploads data to the currently specified FIRStorageReference.
  91. * This is not recommended for large files, and one should instead upload a file from disk.
  92. * @param uploadData The NSData to upload.
  93. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  94. * about the object being uploaded.
  95. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  96. */
  97. // clang-format off
  98. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  99. metadata:(nullable FIRStorageMetadata *)metadata
  100. NS_SWIFT_NAME(putData(_:metadata:));
  101. // clang-format on
  102. /**
  103. * Asynchronously uploads data to the currently specified FIRStorageReference.
  104. * This is not recommended for large files, and one should instead upload a file from disk.
  105. * @param uploadData The NSData to upload.
  106. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  107. * about the object being uploaded.
  108. * @param completion A completion block that either returns the object metadata on success,
  109. * or an error on failure.
  110. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  111. */
  112. // clang-format off
  113. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  114. metadata:(nullable FIRStorageMetadata *)metadata
  115. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  116. NSError *_Nullable error))completion
  117. NS_SWIFT_NAME(putData(_:metadata:completion:));
  118. // clang-format on
  119. /**
  120. * Asynchronously uploads a file to the currently specified FIRStorageReference,
  121. * without additional metadata.
  122. * @param fileURL A URL representing the system file path of the object to be uploaded.
  123. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  124. */
  125. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL NS_SWIFT_NAME(putFile(from:));
  126. /**
  127. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  128. * @param fileURL A URL representing the system file path of the object to be uploaded.
  129. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  130. * about the object being uploaded.
  131. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  132. */
  133. // clang-format off
  134. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
  135. metadata:(nullable FIRStorageMetadata *)metadata
  136. NS_SWIFT_NAME(putFile(from:metadata:));
  137. // clang-format on
  138. /**
  139. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  140. * @param fileURL A URL representing the system file path of the object to be uploaded.
  141. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  142. * about the object being uploaded.
  143. * @param completion A completion block that either returns the object metadata on success,
  144. * or an error on failure.
  145. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  146. */
  147. // clang-format off
  148. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
  149. metadata:(nullable FIRStorageMetadata *)metadata
  150. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  151. NSError *_Nullable error))completion
  152. NS_SWIFT_NAME(putFile(from:metadata:completion:));
  153. // clang-format on
  154. #pragma mark - Downloads
  155. /**
  156. * Asynchronously downloads the object at the FIRStorageReference to an NSData object in memory.
  157. * An NSData of the provided max size will be allocated, so ensure that the device has enough free
  158. * memory to complete the download. For downloading large files, writeToFile may be a better option.
  159. * @param size The maximum size in bytes to download. If the download exceeds this size
  160. * the task will be cancelled and an error will be returned.
  161. * @param completion A completion block that either returns the object data on success,
  162. * or an error on failure.
  163. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  164. */
  165. // clang-format off
  166. - (FIRStorageDownloadTask *)dataWithMaxSize:(int64_t)size
  167. completion:(void (^)(NSData *_Nullable data,
  168. NSError *_Nullable error))completion
  169. NS_SWIFT_NAME(getData(maxSize:completion:));
  170. // clang-format on
  171. /**
  172. * Asynchronously retrieves a long lived download URL with a revokable token.
  173. * This can be used to share the file with others, but can be revoked by a developer
  174. * in the Firebase Console if desired.
  175. * @param completion A completion block that either returns the URL on success,
  176. * or an error on failure.
  177. */
  178. - (void)downloadURLWithCompletion:(void (^)(NSURL *_Nullable URL,
  179. NSError *_Nullable error))completion;
  180. /**
  181. * Asynchronously downloads the object at the current path to a specified system filepath.
  182. * @param fileURL A file system URL representing the path the object should be downloaded to.
  183. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  184. */
  185. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL;
  186. /**
  187. * Asynchronously downloads the object at the current path to a specified system filepath.
  188. * @param fileURL A file system URL representing the path the object should be downloaded to.
  189. * @param completion A completion block that fires when the file download completes.
  190. * Returns an NSURL pointing to the file path of the downloaded file on success,
  191. * or an error on failure.
  192. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  193. */
  194. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL
  195. completion:(nullable void (^)(NSURL *_Nullable URL,
  196. NSError *_Nullable error))completion;
  197. #pragma mark - Metadata Operations
  198. /**
  199. * Retrieves metadata associated with an object at the current path.
  200. * @param completion A completion block which returns the object metadata on success,
  201. * or an error on failure.
  202. */
  203. - (void)metadataWithCompletion:
  204. (void (^)(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error))completion
  205. NS_SWIFT_NAME(getMetadata(completion:));
  206. /**
  207. * Updates the metadata associated with an object at the current path.
  208. * @param metadata An FIRStorageMetadata object with the metadata to update.
  209. * @param completion A completion block which returns the FIRStorageMetadata on success,
  210. * or an error on failure.
  211. */
  212. // clang-format off
  213. - (void)updateMetadata:(FIRStorageMetadata *)metadata
  214. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  215. NSError *_Nullable error))completion
  216. NS_SWIFT_NAME(updateMetadata(_:completion:));
  217. // clang-format on
  218. #pragma mark - Delete
  219. /**
  220. * Deletes the object at the current path.
  221. * @param completion A completion block which returns nil on success, or an error on failure.
  222. */
  223. - (void)deleteWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  224. @end
  225. NS_ASSUME_NONNULL_END