FIRStorageReference.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "FIRStorageSwiftNameSupport.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. FIR_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 FIR_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. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  99. metadata:(nullable FIRStorageMetadata *)metadata
  100. FIR_SWIFT_NAME(putData(_:metadata:));
  101. /**
  102. * Asynchronously uploads data to the currently specified FIRStorageReference.
  103. * This is not recommended for large files, and one should instead upload a file from disk.
  104. * @param uploadData The NSData to upload.
  105. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  106. * about the object being uploaded.
  107. * @param completion A completion block that either returns the object metadata on success,
  108. * or an error on failure.
  109. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  110. */
  111. - (FIRStorageUploadTask *)putData:(NSData *)uploadData
  112. metadata:(nullable FIRStorageMetadata *)metadata
  113. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  114. NSError *_Nullable error))completion
  115. FIR_SWIFT_NAME(putData(_:metadata:completion:));
  116. /**
  117. * Asynchronously uploads a file to the currently specified FIRStorageReference,
  118. * without additional metadata.
  119. * @param fileURL A URL representing the system file path of the object to be uploaded.
  120. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  121. */
  122. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL FIR_SWIFT_NAME(putFile(from:));
  123. /**
  124. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  125. * @param fileURL A URL representing the system file path of the object to be uploaded.
  126. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  127. * about the object being uploaded.
  128. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  129. */
  130. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL metadata:(nullable FIRStorageMetadata *)metadata
  131. FIR_SWIFT_NAME(putFile(from:metadata:));
  132. /**
  133. * Asynchronously uploads a file to the currently specified FIRStorageReference.
  134. * @param fileURL A URL representing the system file path of the object to be uploaded.
  135. * @param metadata FIRStorageMetadata containing additional information (MIME type, etc.)
  136. * about the object being uploaded.
  137. * @param completion A completion block that either returns the object metadata on success,
  138. * or an error on failure.
  139. * @return An instance of FIRStorageUploadTask, which can be used to monitor or manage the upload.
  140. */
  141. - (FIRStorageUploadTask *)putFile:(NSURL *)fileURL
  142. metadata:(nullable FIRStorageMetadata *)metadata
  143. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  144. NSError *_Nullable error))completion
  145. FIR_SWIFT_NAME(putFile(from:metadata:completion:));
  146. #pragma mark - Downloads
  147. /**
  148. * Asynchronously downloads the object at the FIRStorageReference to an NSData object in memory.
  149. * An NSData of the provided max size will be allocated, so ensure that the device has enough free
  150. * memory to complete the download. For downloading large files, writeToFile may be a better option.
  151. * @param size The maximum size in bytes to download. If the download exceeds this size
  152. * the task will be cancelled and an error will be returned.
  153. * @param completion A completion block that either returns the object data on success,
  154. * or an error on failure.
  155. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  156. */
  157. - (FIRStorageDownloadTask *)dataWithMaxSize:(int64_t)size
  158. completion:(void (^)(NSData *_Nullable data,
  159. NSError *_Nullable error))completion
  160. FIR_SWIFT_NAME(getData(maxSize:completion:));
  161. /**
  162. * Asynchronously retrieves a long lived download URL with a revokable token.
  163. * This can be used to share the file with others, but can be revoked by a developer
  164. * in the Firebase Console if desired.
  165. * @param completion A completion block that either returns the URL on success,
  166. * or an error on failure.
  167. */
  168. - (void)downloadURLWithCompletion:(void (^)(NSURL *_Nullable URL,
  169. NSError *_Nullable error))completion;
  170. /**
  171. * Asynchronously downloads the object at the current path to a specified system filepath.
  172. * @param fileURL A file system URL representing the path the object should be downloaded to.
  173. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  174. */
  175. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL;
  176. /**
  177. * Asynchronously downloads the object at the current path to a specified system filepath.
  178. * @param fileURL A file system URL representing the path the object should be downloaded to.
  179. * @param completion A completion block that fires when the file download completes.
  180. * Returns an NSURL pointing to the file path of the downloaded file on success,
  181. * or an error on failure.
  182. * @return An FIRStorageDownloadTask that can be used to monitor or manage the download.
  183. */
  184. - (FIRStorageDownloadTask *)writeToFile:(NSURL *)fileURL
  185. completion:(nullable void (^)(NSURL *_Nullable URL,
  186. NSError *_Nullable error))completion;
  187. #pragma mark - Metadata Operations
  188. /**
  189. * Retrieves metadata associated with an object at the current path.
  190. * @param completion A completion block which returns the object metadata on success,
  191. * or an error on failure.
  192. */
  193. - (void)metadataWithCompletion:(void (^)(FIRStorageMetadata *_Nullable metadata,
  194. NSError *_Nullable error))completion
  195. FIR_SWIFT_NAME(getMetadata(completion:));
  196. /**
  197. * Updates the metadata associated with an object at the current path.
  198. * @param metadata An FIRStorageMetadata object with the metadata to update.
  199. * @param completion A completion block which returns the FIRStorageMetadata on success,
  200. * or an error on failure.
  201. */
  202. - (void)updateMetadata:(FIRStorageMetadata *)metadata
  203. completion:(nullable void (^)(FIRStorageMetadata *_Nullable metadata,
  204. NSError *_Nullable error))completion
  205. FIR_SWIFT_NAME(updateMetadata(_:completion:));
  206. #pragma mark - Delete
  207. /**
  208. * Deletes the object at the current path.
  209. * @param completion A completion block which returns nil on success, or an error on failure.
  210. */
  211. - (void)deleteWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  212. @end
  213. NS_ASSUME_NONNULL_END