AWS SDK for C++  0.12.9
AWS SDK for C++
S3FileRequest.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License").
6  * You may not use this file except in compliance with the License.
7  * A copy of the License is located at
8  *
9  * http://aws.amazon.com/apache2.0
10  *
11  * or in the "license" file accompanying this file. This file is distributed
12  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 #pragma once
18 
20 
21 #include <aws/s3/S3Client.h>
22 
23 #include <list>
24 #include <fstream>
25 #include <functional>
26 
27 namespace Aws
28 {
29 namespace S3
30 {
31  class S3Client;
32 } // namespace S3
33 
34 namespace Transfer
35 {
36 
37 class TransferClient;
38 
40 {
41 public:
42 
43  using S3FileCompletionCallback = std::function < void() > ;
44 
45  S3FileRequest(const Aws::String& fileName, const Aws::String& bucketName, const Aws::String& keyName, const std::shared_ptr<Aws::S3::S3Client>& s3Client);
46  virtual ~S3FileRequest();
47 
48  // S3FileRequest defines this as progress out of file size - inherited classes that can't conform to this for some reason should derive their own
49  virtual float GetProgress() const;
50 
51  // Simple accessors to return values passed into constructor
52  const Aws::String& GetBucketName() const;
53  const Aws::String& GetFileName() const;
54 
55  // Keyname passed in, or filename if not provided
56  const Aws::String& GetKeyName() const;
57 
58  // IsDone is set when the operation has completed with or without success
59  bool IsDone() const;
60 
61  // Set when the particular operation has entirely completed with success
62  bool CompletedSuccessfully() const;
63 
64  // Synchronous call to put the request into a CheckDone/Sleep 1 second loop for up to UPLOAD_WAIT_TIMEOUT (100 default) seconds
65  // Any progress will restart the counter to UPLOAD_WAIT_TIMEOUT back to 0 (Relies on a measurable incremental changes in GetProgress)
66  virtual bool WaitUntilDone() const;
67 
68  // Human readable failure string routed through CompletionFailure
69  const Aws::String& GetFailure() const;
70 
71  void SetLastFailure(const char* failureString);
72 
73  // Different file requests will have different meanings for ready - some may need S3 buckets created, some may need files opened
74  virtual bool IsReady() const = 0;
75 
76  // For uploads, look at the file on disk, for downloads, request content manifest from S3
77  inline virtual uint64_t GetFileSize() const { return m_fileSize; }
78 
79  uint64_t GetProgressAmount() const;
80 
81  // Add a callback to be fired on CompletionSuccess
82  void AddCompletionCallback(S3FileCompletionCallback addCallback);
83 
84 protected:
85 
86  const std::shared_ptr<Aws::S3::S3Client>& GetS3Client() const;
87 
88  void Cancel();
89 
90  // Anything the derived class needs to do in particular to clean up the request
91  virtual bool DoCancelAction() = 0;
92 
93  virtual void SetDone();
94  void SetCompleted();
95  void CompletionSuccess();
96  virtual void CompletionFailure(const char* failureStr);
97 
98  inline virtual void SetFileSize(uint64_t curSize) { m_fileSize = curSize; }
99 
100  void ClearProgress();
101  void RegisterProgress(int64_t progressAmount);
102 
103 private:
104 
105  void FireCompletionCallbacks();
106 
107  Aws::String m_fileName;
108  Aws::String m_bucketName;
109  Aws::String m_keyName;
110  Aws::String m_contentType;
111 
112  // If we don't complete successfully let's store a human readable reason
113  Aws::String m_failureString;
114 
115  std::shared_ptr<Aws::S3::S3Client> m_S3Client;
116 
117  std::atomic<bool> m_isDone;
118  std::atomic<bool> m_completedSuccessfully;
119 
120  std::mutex m_callbackMutex;
121 
122  bool m_cancelled;
123  uint64_t m_fileSize;
124  std::atomic<uint64_t> m_progress;
125  Aws::List<S3FileCompletionCallback> m_completionCallbacks;
126 };
127 
128 } // namespace Transfer
129 } // namespace Aws
std::function< void() > S3FileCompletionCallback
Definition: S3FileRequest.h:43
virtual void SetFileSize(uint64_t curSize)
Definition: S3FileRequest.h:98
virtual uint64_t GetFileSize() const
Definition: S3FileRequest.h:77
std::list< T, Aws::Allocator< T > > List
Definition: AWSList.h:27
#define AWS_TRANSFER_API
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
JSON (JavaScript Object Notation).