|
1 | | -function cloudDatasetId = uploadDataset(ndiDataset, syncOptions) |
| 1 | +function [success, cloudDatasetId, message] = uploadDataset(ndiDataset, syncOptions, options) |
2 | 2 | % UPLOADDATASET - upload a dataset to NDI cloud |
3 | 3 | % |
4 | | - % DATASETID = ndi.cloud.UPLOADDATASET(ndiDataset) |
| 4 | + % [SUCCESS, DATASETID, MESSAGE] = ndi.cloud.UPLOADDATASET(ndiDataset, SYNCOPTIONS, ... |
| 5 | + % NAME/VALUE PAIRS) |
5 | 6 | % |
6 | | - % Upload an ndi.dataset object to NDI Cloud. The DATASETID on |
7 | | - % NDI Cloud is returned. |
| 7 | + % Upload an ndi.dataset object to NDI Cloud. |
8 | 8 | % |
9 | | - % Example: |
10 | | - % ndi.cloud.upload.newDataset(ndiDataset) |
| 9 | + % This function uploads all documents and associated data files for a given |
| 10 | + % NDIDATASET to the NDI cloud. |
| 11 | + % |
| 12 | + % Inputs: |
| 13 | + % ndiDataset - The ndi.dataset object to be uploaded. |
| 14 | + % syncOptions - An ndi.cloud.sync.SyncOptions object for additional configuration. |
| 15 | + % |
| 16 | + % By default, this function will not re-upload a dataset if it already exists |
| 17 | + % on the remote server. See the 'uploadAsNew' option to override this behavior. |
| 18 | + % |
| 19 | + % The function returns a boolean SUCCESS flag, the CLOUDDATASETID of the |
| 20 | + % created remote dataset, and a MESSAGE string that will contain an error |
| 21 | + % message if SUCCESS is false. |
| 22 | + % |
| 23 | + % It can be configured with the following NAME/VALUE pairs: |
| 24 | + % | Name | Description | |
| 25 | + % |------------------------------|--------------------------------------------------------------------------| |
| 26 | + % | 'uploadAsNew' | (logical) If true, any existing remote dataset will be deleted and a new | |
| 27 | + % | | one will be created. Default is false. If a remote dataset exists and | |
| 28 | + % | | this is false, the function will return an error. | |
| 29 | + % | 'skipMetadataEditorMetadata' | (logical) If true, the function will skip generating metadata from the | |
| 30 | + % | | dataset. Default is false. If you use this option, you must also provide | |
| 31 | + % | | 'remoteDatasetName'. | |
| 32 | + % | 'remoteDatasetName' | (char) The name to be assigned to the dataset on the remote server. This | |
| 33 | + % | | is *required* if 'skipMetadataEditorMetadata' is true. | |
| 34 | + % |
| 35 | + % See also: ndi.cloud.sync.SyncOptions, ndi.cloud.downloadDataset |
11 | 36 | % |
12 | 37 |
|
13 | 38 | arguments |
14 | 39 | ndiDataset (1,1) ndi.dataset |
15 | 40 | syncOptions.?ndi.cloud.sync.SyncOptions |
| 41 | + options.uploadAsNew (1,1) logical = false |
| 42 | + options.skipMetadataEditorMetadata (1,1) logical = false |
| 43 | + options.remoteDatasetName (1,:) char = '' |
| 44 | + end |
| 45 | + |
| 46 | + success = false; |
| 47 | + cloudDatasetId = ''; |
| 48 | + message = ''; |
| 49 | + |
| 50 | + [cloudDatasetId, remote_doc] = ndi.cloud.internal.getCloudDatasetIdForLocalDataset(ndiDataset); |
| 51 | + |
| 52 | + if ~isempty(cloudDatasetId) & ~options.uploadAsNew, |
| 53 | + message = ['Dataset has already been uploaded, and "uploadAsNew" is false.']; |
| 54 | + return; |
| 55 | + elseif ~isempty(cloudDatasetId) & options.uploadAsNew, |
| 56 | + [delete_success, delete_message] = ndi.cloud.api.admin.deleteDataset(cloudDatasetId); |
| 57 | + if ~delete_success |
| 58 | + message = ['Could not delete existing remote dataset: ' delete_message]; |
| 59 | + return; |
| 60 | + end |
| 61 | + ndiDataset.database_rm(remote_doc); |
| 62 | + cloudDatasetId = ''; |
16 | 63 | end |
17 | 64 |
|
18 | 65 | % Step 1: Create the dataset record on NDI Cloud and insert the metadata |
19 | | - |
20 | | - % Step 1a: Retrieve metadata from the dataset |
21 | | - metadata_struct = ndi.database.metadata_ds_core.ndidataset2metadataeditorstruct(ndiDataset); |
| 66 | + if options.skipMetadataEditorMetadata |
| 67 | + if isempty(options.remoteDatasetName) |
| 68 | + message = 'If skipMetadataEditorMetadata is true, remoteDatasetName cannot be empty.'; |
| 69 | + return; |
| 70 | + end |
| 71 | + cloud_dataset_info.name = options.remoteDatasetName; |
| 72 | + else |
| 73 | + % Step 1a: Retrieve metadata from the dataset |
| 74 | + metadata_struct = ndi.database.metadata_ds_core.ndidataset2metadataeditorstruct(ndiDataset); |
22 | 75 |
|
23 | | - % Step 1b: Convert metadata structure to NDI Cloud Dataset info |
24 | | - cloud_dataset_info = ndi.cloud.utility.createCloudMetadataStruct(metadata_struct); |
| 76 | + % Step 1b: Convert metadata structure to NDI Cloud Dataset info |
| 77 | + cloud_dataset_info = ndi.cloud.utility.createCloudMetadataStruct(metadata_struct); |
| 78 | + end |
25 | 79 |
|
26 | 80 | % Step 1c: Create new NDI Cloud Dataset |
27 | | - [success, answer] = ndi.cloud.api.datasets.createDataset(cloud_dataset_info); |
28 | | - if ~success |
29 | | - error(['Failed to create dataset: ' answer.message]); |
| 81 | + [success_create, answer] = ndi.cloud.api.datasets.createDataset(cloud_dataset_info); |
| 82 | + if ~success_create |
| 83 | + message = ['Failed to create dataset: ' answer.message]; |
| 84 | + return; |
30 | 85 | end |
31 | 86 | cloudDatasetId = answer.dataset_id; |
32 | 87 |
|
|
37 | 92 | % Step 2: Upload documents |
38 | 93 | if syncOptions.Verbose, disp('Uploading dataset documents...'); end |
39 | 94 | dataset_documents = ndiDataset.database_search( ndi.query('','isa','base') ); |
40 | | - ndi.cloud.upload.uploadDocumentCollection(cloudDatasetId, dataset_documents) |
| 95 | + ndi.cloud.upload.uploadDocumentCollection(cloudDatasetId, dataset_documents, "onlyUploadMissing", true) |
41 | 96 |
|
42 | 97 | % Step 3: Upload files |
43 | | - ndi.cloud.sync.internal.uploadFilesForDatasetDocuments( ... |
| 98 | + [success_upload, message_upload] = ndi.cloud.sync.internal.uploadFilesForDatasetDocuments( ... |
44 | 99 | cloudDatasetId, ndiDataset, dataset_documents, ... |
45 | 100 | "Verbose", syncOptions.Verbose, ... |
46 | | - "FileUploadStrategy", syncOptions.FileUploadStrategy) |
| 101 | + "FileUploadStrategy", syncOptions.FileUploadStrategy, "onlyMissing", true); |
| 102 | + if ~success_upload |
| 103 | + message = message_upload; |
| 104 | + return; |
| 105 | + end |
| 106 | + |
| 107 | + success = true; |
47 | 108 | end |
0 commit comments