/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include "AWSBehaviorS3Download.h" #include /// To use a specific AWS API request you have to include each of these. #include AZ_PUSH_DISABLE_WARNING(4251 4355 4996, "-Wunknown-warning-option") #include #include AZ_POP_DISABLE_WARNING #include namespace CloudGemAWSScriptBehaviors { static const char* DOWNLOAD_CLASS_TAG = "AWS:Primitive:AWSBehaviorS3Download"; AWSBehaviorS3Download::AWSBehaviorS3Download() : m_bucketName(), m_keyName(), m_localFileName() { } void AWSBehaviorS3Download::ReflectSerialization(AZ::SerializeContext* serializeContext) { if (serializeContext) { serializeContext->Class() ->Field("bucketName", &AWSBehaviorS3Download::m_bucketName) ->Field("keyName", &AWSBehaviorS3Download::m_keyName) ->Field("localFileName", &AWSBehaviorS3Download::m_localFileName) ->Version(1); } } void AWSBehaviorS3Download::ReflectBehaviors(AZ::BehaviorContext* behaviorContext) { behaviorContext->Class("AWSBehaviorS3Download") ->Method("Download", &AWSBehaviorS3Download::Download, nullptr, "S3 download operation on AWS") ->Property("bucketName", nullptr, BehaviorValueSetter(&AWSBehaviorS3Download::m_bucketName)) ->Property("keyName", nullptr, BehaviorValueSetter(&AWSBehaviorS3Download::m_keyName)) ->Property("localFileName", nullptr, BehaviorValueSetter(&AWSBehaviorS3Download::m_localFileName)); behaviorContext->EBus("AWSBehaviorS3DownloadNotificationsBus") ->Handler(); } void AWSBehaviorS3Download::ReflectEditParameters(AZ::EditContext* editContext) { editContext->Class("AWSBehaviorS3Download", "Wraps AWS S3 download functionality") ->DataElement(AZ::Edit::UIHandlers::Default, &AWSBehaviorS3Download::m_bucketName, "BucketName", "The name of the bucket to use") ->DataElement(AZ::Edit::UIHandlers::Default, &AWSBehaviorS3Download::m_keyName, "KeyName", "The name of the file on S3 to be downloaded") ->DataElement(AZ::Edit::UIHandlers::Default, &AWSBehaviorS3Download::m_localFileName, "LocalFileName", "The local file name to use for the downloaded object"); } void AWSBehaviorS3Download::Download() { if (m_localFileName.empty()) { EBUS_EVENT(AWSBehaviorS3DownloadNotificationsBus, OnError, "Please specify a local file name"); return; } if (AZ::IO::FileIOBase::GetInstance()->IsReadOnly(m_localFileName.c_str())) { EBUS_EVENT(AWSBehaviorS3DownloadNotificationsBus, OnError, "File is read only"); return; } if (m_bucketName.empty()) { EBUS_EVENT(AWSBehaviorS3DownloadNotificationsBus, OnError, "Please specify a bucket name"); return; } if (m_keyName.empty()) { EBUS_EVENT(AWSBehaviorS3DownloadNotificationsBus, OnError, "Please specify a key name"); return; } using S3DownloadRequestJob = AWS_API_REQUEST_JOB(S3, GetObject); S3DownloadRequestJob::Config config(S3DownloadRequestJob::GetDefaultConfig()); AZStd::string region; EBUS_EVENT_RESULT(region, CloudGemFramework::CloudCanvasMappingsBus, GetLogicalToPhysicalResourceMapping, "region"); config.region = region.c_str(); auto job = S3DownloadRequestJob::Create( [](S3DownloadRequestJob* job) // OnSuccess handler { AZStd::function notifyOnMainThread = []() { AWSBehaviorS3DownloadNotificationsBus::Broadcast(&AWSBehaviorS3DownloadNotificationsBus::Events::OnSuccess, "File Downloaded"); }; AZ::TickBus::QueueFunction(notifyOnMainThread); }, [](S3DownloadRequestJob* job) // OnError handler { Aws::String errorMessage = job->error.GetMessage(); AZStd::function notifyOnMainThread = [errorMessage]() { AWSBehaviorS3DownloadNotificationsBus::Broadcast(&AWSBehaviorS3DownloadNotificationsBus::Events::OnError, errorMessage.c_str()); }; AZ::TickBus::QueueFunction(notifyOnMainThread); }, &config ); AZStd::string bucketName; EBUS_EVENT_RESULT(bucketName, CloudGemFramework::CloudCanvasMappingsBus, GetLogicalToPhysicalResourceMapping, m_bucketName.c_str()); job->request.SetBucket(Aws::String(bucketName.c_str())); job->request.SetKey(Aws::String(m_keyName.c_str())); Aws::String awsLocalFileName (m_localFileName.c_str()); job->request.SetResponseStreamFactory([awsLocalFileName]() { return Aws::New(DOWNLOAD_CLASS_TAG, awsLocalFileName.c_str(), std::ios_base::out | std::ios_base::in | std::ios_base::binary | std::ios_base::trunc); }); job->Start(); } }