I am generating a .csv file for further storing in a s3 bucket using .net c# Lambda function.
This is the process i follow:
Generate the .csv and store it in /tmp/ folder of the lambda function execution.
In this step im not sure if it is really saving the file in that path.
//filepath = #"/tmp/test.csv"
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#filepath, true))
{
file.WriteLine(ID + "," + workItemType + "," + title + "," + assignedTo + "," + state + "," + iterationPath);
Console.WriteLine("Successfully added");
}
}
catch (Exception ex)
{
throw new ApplicationException(" somethings wrong: ", ex);
}
Upload the file to s3 bucket.
try
{
await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = "mys3bucket",
Key = "test.csv",
ContentType = #"/tmp/test.csv"
});
await Task.CompletedTask;
}
catch (Exception ex)
{
Console.WriteLine("Exception in PutS3Object:" + ex.Message); ;
}
In this last step i get this error message:
Exception in PutS3Object:The format of value '\tmp\test.csv' is invalid.
What i am doing wrong?
You need to send the data to include in the csv file:
await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = "mys3bucket",
Key = "test.csv",
ContentBody = DATAINSTRINGFORMAT,
ContentType = #"text/csv"
});
or as filepath to send:
await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = "mys3bucket",
Key = "test.csv",
FilePath = FILEPATHONYOURTEMPFOLDER,
ContentType = #"text/csv"
});
Related
I work on ASP.NET Core 2.2 Web API and face an issue: I can't use replace function to change the name property of a selected file that I get when uploaded.
When I try like this:
string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
I get an error
Iform file doesn't contain definition for replace and no accessible extension method Replace accepting first argument of iformfile
Full sample is here:
[HttpPost, DisableRequestSizeLimit]
public IActionResult Upload()
{
try
{
var DisplayFileName = Request.Form.Files[0];
string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
string Month = DateTime.Now.Month.ToString();
string DirectoryCreate = myValue1 + Month;
Path.Combine(Directory.GetCurrentDirectory(), folderName);
if (!Directory.Exists(DirectoryCreate))
{
Directory.CreateDirectory(DirectoryCreate);
}
if (DisplayFileName.Length > 0)
{
var filedata = ContentDispositionHeaderValue.Parse(Request.Form.Files[0].ContentDisposition).FileName.Trim('"');
var dbPath = Path.Combine(DirectoryCreate, fileName);
using (var stream = new FileStream(dbPath, FileMode.Create))
{
Request.Form.Files[0].CopyTo(stream);
}
return Ok(new { dbPath });
}
else
{
return BadRequest();
}
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex}");
}
}
How to solve this issue?
sample
suppose i select file developed.xlsx
then after use replace or any way result will be
developed-sddfn78888.xlsx
You can use System.IO.Path to get filename and get file extension from request files.
Change this
string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
To
string filename = Path.GetFileName(DisplayFileName.FileName);
string fileExtension = Path.GetExtension(DisplayFileName.FileName);
string newFileName = $"{filename}-{Guid.NewGuid().ToString()}{fileExtension}";
Otherwise, you could modify your code to
string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
Using the Google Drive API to upload files. Multiple same console app is running at the same time to upload different parts of files in the folder and their files don't overlap. This hits the quota limit. Then a while-try-catch is implemented to re-execute the query whenever it throws the exception because of the quota limit. The list and create directory method works well but not the upload (i.e. create) method. Some files are missing when i checked from the Google Drive site
Tried using FileStream instead of MemoryStream but it seems not related.
public static Google.Apis.Drive.v3.Data.File uploadFile(DriveService _service, string _uploadFile)
{
bool again = true;
string[] p = _uploadFile.Split('/');
if (System.IO.File.Exists("C:/"+_uploadFile))
{
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
body.Name = System.IO.Path.GetFileName(p[p.Length-1]);
body.Description = "";
body.MimeType = GetMimeType("C:/"+_uploadFile);
body.Parents = new List<string>() { ID };
// File's content.
System.IO.FileStream stream = new System.IO.FileStream("C:/" + _uploadFile, FileMode.Open);
try
{
FilesResource.CreateMediaUpload request = _service.Files.Create(body, stream, GetMimeType("C:/" + _uploadFile));
while (again)
{
try
{
request.Upload();
again = false;
}
catch (Exception e)
{
Console.WriteLine("uploadFile: "+p[p.Length-1]);
}
}
return body;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
else
{
Console.WriteLine("File does not exist: " + _uploadFile);
return null;
}
}
I have used the following below code to convert the zip file to enc file but its not working perfectly, please help me to solve this issue
Also if there is any third party tool to convert the zip to enc file please mention the details about it .
Thanks in Advance
try
{
string startPath = "";
string path = #"F:\Augustin\UpTest\Upload\";
DirectoryInfo objdirinfovikramfile = new DirectoryInfo(path);
if (objdirinfovikramfile.Exists)
{
errorcheck = "a";
foreach (DirectoryInfo objdirenmvikramfile in objdirinfovikramfile.GetDirectories())
{
string checkvikramfile = "";
checkvikramfile = objdirenmvikramfile.Name;
startPath = path + checkvikramfile + "\\";
string zipPath = path + checkvikramfile + ".zip";
string sub = zipPath.Split('\\').Last();
Boolean decrypt = false;
FileManager obj = new FileManager();
Int64 secure_count = 0;
while (secure_count <= 3 && !decrypt)
{
decrypt = obj_viki1.Decryption(zipPath, zipPath.Replace(".zip", ".enc"), "dietcoke1"); //DLL Function
secure_count++;
}
if (secure_count > 3 && !decrypt)
{
File.Copy(zipPath, path + "\\FAILED\\" + zipPath.Split('\\').Last(),true );
File.Copy(zipPath, path + "\\FAILED\\" + zipPath.Split('\\').Last(), true);
}
else
{
File.Copy(zipPath, path + "\\SUCCESS\\" + zipPath.Split('\\').Last(), true);
}
}
}
}
catch (Exception ex)
{
Error_log("FileProcess Enc: " + errorcheck + " ", ex.ToString());
Application.Exit();
}
When I retrieve all Files (and Folders) of my GoogleDrive Account I should get something like 1500 List elements back, but I get a bit more than 3000 back. I looked into the List and found that some files are 2-3 times in it. Why is that?
Here is the code I use to retrieve the files:
public async Task<List<File>> RetrieveAllFilesAsList(DriveService service, string query = null)
{
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
if (query != null)
{
request.Q = query;
}
do
{
try
{
FileList files = await request.ExecuteAsync();
result.AddRange(files.Items);
request.PageToken = files.NextPageToken;
}
catch (Exception e)
{
Console.WriteLine("An error occurred (from RetrieveAllFilesAsList): " + e.Message);
request.PageToken = null;
}
}
while (!String.IsNullOrEmpty(request.PageToken));
return result;
}
Update1:
public async Task<List<File>> RetrieveAllFilesAsList(DriveService service, string query = null)
{
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
request.MaxResults = 1000;
if (query != null)
{
request.Q = query + " AND trashed=false";
}
else
{
request.Q = "trashed=false";
}
do
{
try
{
FileList files = await request.ExecuteAsync();
result.AddRange(files.Items);
request.PageToken = files.NextPageToken;
}
catch (Exception e)
{
Console.WriteLine("An error occurred (from RetrieveAllFilesAsList): " + e.Message);
request.PageToken = null;
}
}
while (!String.IsNullOrEmpty(request.PageToken));
int i;
for (i = 0; i < result.Count; i++ )
{
System.IO.File.AppendAllText(#"C:\Users\carl\Desktop\log.txt", result[i].Id + "\t" + result[i].Title + "\t" + result[i].ExplicitlyTrashed.ToString() + "\r\n");
}
// prints 3120 Lines
System.IO.File.AppendAllText(#"C:\Users\carl\Desktop\log.txt", "" + i + Environment.NewLine);
//Count = 3120
System.IO.File.AppendAllText(#"C:\Users\carl\Desktop\log.txt", "" + result.Count);
return result;
}
Word failed to give me the right the linecount, so I did it over my Function.
But I can find the FileId 2-3 times in the File.
I cannot write on the comments yet, so according to the API
from google
"Note: This method returns all files by default. This includes files with trashed=true in the results. Use the trashed=false query parameter to filter these from the results."
So can you check what url of the rest api is actually being called? It seems you need to put some filters on the List method.
i want to upload a list of .txt, and keep them in a custom folder on skydrive
like Someone's Account -> Skydrive -> custom folder ('testfile')
i have tried
LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive/testfile", new Uri("/shared/transfers/" + t, UriKind.Relative),OverwriteOption.Overwrite);,
but it doesn't work at all, it give me an error of:
The URL contains the path 'testfile', which isn't supported.
if i need to get folder ID to upload the file, how do i get the ID?
here is my code:
private async void button_Click(object sender, EventArgs e)
{
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
var temptempout = new List<String>(isoStore.GetFileNames("*").ToList());
int total = temptempout.Count;
int now = 0;
if (temptempout.Count > 0)
{
ShowTextDebug.Text = "Uploaded 0 of " + total + " files";
foreach (String t in temptempout)
{
using (var fileStream = isoStore.OpenFile(t, FileMode.Open, FileAccess.Read))
{
try
{
LiveOperationResult res = await client.BackgroundUploadAsync("me/skydrive/testfile",
new Uri("/shared/transfers/" + t, UriKind.Relative),
OverwriteOption.Overwrite
);
}
catch (Exception err)
{
String rrtt = "there is an error while uploading txt " + err.Message;
MessageBox.Show(rrtt, "error", MessageBoxButton.OK);
}
}
now++;
ShowTextDebug.Text = "Uploaded " + now + " of " + total + " files";
}
ShowTextDebug.Text += "\nupload complete";
}
else
{
MessageBox.Show("no txt exist", "error", MessageBoxButton.OK);
}
}
thanks for helping me
You need to get the folder id first. You can do it as follows:
private async Task<string> GetSkyDriveFolderID(string folderName)
{
client = App.LiveClient;
LiveOperationResult operationResult = await client.GetAsync("me/skydrive/files?filter=folders");
var iEnum = operationResult.Result.Values.GetEnumerator();
iEnum.MoveNext();
var folders = iEnum.Current as IEnumerable;
foreach (dynamic v in folders)
{
if (v.name == folderName)
{
return v.id as string;
}
}
return null;
}
Call this method before uploading the file to get the folder id:
string folderId = await GetSkyDriveFolderId("folderName");
LiveOperationResult res = await client.BackgroundUploadAsync(folderId, new Uri("/shared/transfers/" + t, UriKind.Relative), OverwriteOption.Overwrite);