Uploading to S3 but nothing appears in the Web Console - c#

I am trying to upload CSV files to AWS S3. My code has no syntax errors and doesn't cause any exceptions but the CSV files do not appear in web console. I also want to organise the CSV files by date. Here's the code:
string[] files = Directory.GetFiles(folder, "*.csv*", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
PutObjectRequest request = new PutObjectRequest();
request.BucketName = "WorkFolder";
request.Key = "CSV/" + date + "/";
request.FilePath = file;
s3client.PutObject(request);
response = s3client.PutObject(request);
}

I found the answer. I was missing the "file name" in Key property. The correct code is below:
PutObjectRequest request = new PutObjectRequest();
request.BucketName = "WorkFolder";
request.Key = "CSV/" + date + "/" + file; // where file is the name of the file
request.FilePath = s;
s3client.PutObject(request);

Related

I cannot delete a file via FTP

I am trying to delete all the files in a folder via FTP. Below is the code I am trying.
Files is an array of strings each one if the name of a file in the folder with its extension.
When I run it I get an a reply of 206 but when I look in the folder all the files remain. I tried variations of the code below, including adding a delay, but still cannot delete the files. What have I missed?
foreach (var FileName2 in Files)
{
if (File.Exists(txtbx_save_backup_to.Text + "/" + FileName2))
{
FtpWebRequest Delrequest = (FtpWebRequest)WebRequest.Create(ftp_address + "/Temp/Backup/" + FileName2);
Delrequest.Credentials = new NetworkCredential(username, password);
Delrequest.Method = WebRequestMethods.Ftp.DeleteFile;
Task.Delay(1000);
using (FtpWebResponse response2 = (FtpWebResponse)request.GetResponse())
{
rchtxtbx_backup_comms.AppendText("Deleted File, status " + response2.StatusDescription + "\r");
rchtxtbx_backup_comms.ScrollToCaret();
}
}
}
The comments above gave me the clue I needed in a roundabout way so thanks to you for answering.
I had missed out the part to "action" the delete request. So I added the following and now it works.
WebResponse GetResponse = Delrequest.GetResponse();
Stream GResponseStream = GetResponse.GetResponseStream();
I removed the wait and the complete code is now
foreach (var FileName2 in Files)
{
if (File.Exists(txtbx_save_backup_to.Text + "/" + FileName2))
{
FtpWebRequest Delrequest = (FtpWebRequest)WebRequest.Create(ftp_address + "/Temp/Backup/" + FileName2);
Delrequest.Credentials = new NetworkCredential(username, password);
Delrequest.Method = WebRequestMethods.Ftp.DeleteFile;
//Action request
WebResponse GetResponse = Delrequest.GetResponse();
Stream GResponseStream = GetResponse.GetResponseStream();
using (FtpWebResponse response2 = (FtpWebResponse)request.GetResponse())
{
rchtxtbx_backup_comms.AppendText("Deleted File, status " + response2.StatusDescription + "\r");
rchtxtbx_backup_comms.ScrollToCaret();
}
GResponseStream.Close();
}
}

How to download a file to azure storage?

When i click the download link it send me to a error page trying to debug it its telling me that my given paths format is not supported
In my controller class:
public async Task<ActionResult> DownloadBlob(string file, string extension)
{
string downloadPath = await repo.DownloadBlobAsync(file, extension);
return Json(downloadPath);
}
In My Blob Storage class:
public async Task<string> DownloadBlobAsync (string file, string fileExtension)
{
_cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);
var path = downloadPath + file + "." + fileExtension;
using (var fileStream = System.IO.File.OpenWrite(path))
{
fileStream.Position = 1;
//fileStream.Seek(0, SeekOrigin.Begin);
await blockBlob.DownloadToStreamAsync(fileStream);
return path;
}
}
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: The given path's format is not supported
The source of the error :
using (var fileStream = System.IO.File.OpenWrite(path))
below there is the download path value:
public class BlobStorageRepository : IBlobStorageRepository
{
private StorageCredentials _storageCredentialsx;
private CloudStorageAccount _cloudStorageAccountx;
private CloudBlobContainer _cloudBlobContainerx;
private CloudBlobClient _cloudBlobClientx;
private string containerNamex = "mycontainer";
private string downloadPath = #"D:\Images\";
public BlobStorageRepository()
{
string accountName = "Account name";
string keyx = "account key";
_storageCredentialsx = new StorageCredentials(accountName, keyx); //set the azure storage credentals
_cloudStorageAccountx = new CloudStorageAccount(_storageCredentialsx, true); //connect to storage service
_cloudBlobClientx = _cloudStorageAccountx.CreateCloudBlobClient(); //create the blob service client
_cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex); //contains all blobs for container
How to download a file to azure storage?
If you want to download the blob file to the client side. You could use the following code to do that.
var blockBlob = container.GetBlockBlobReference(file + "." + fileExtension);
blockBlob.FetchAttributes();
var contentType = blockBlob.Properties.ContentType;
MemoryStream memStream = new MemoryStream();
blockBlob.DownloadToStream(memStream);
var response = HttpContext.Response;
response.ContentType = contentType;
response.AddHeader("Content-Disposition", "Attachment; filename=" + file + "." + fileExtension);
response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
response.BinaryWrite(memStream.ToArray());

get downloaded file from URL and Illegal characters in path

string uri = "https://sometest.com/l/admin/ical.html?t=TD61C7NibbV0m5bnDqYC_q";
string filePath = "D:\\Data\\Name";
WebClient webClient = new WebClient();
webClient.DownloadFile(uri, (filePath + "/" + uri.Substring(uri.LastIndexOf('/'))));
/// filePath + "/" + uri.Substring(uri.LastIndexOf('/')) = "D:\\Data\\Name//ical.html?t=TD61C7NibbV0m5bnDqYC_q"
Accesing the entire ( string ) uri, a .ical file will be automatically downloaded... The file name is room113558101.ics ( not that this will help ).
How can I get the file correctly?
You are building your filepath in a wrong way, which results in invalid file name (ical.html?t=TD61C7NibbV0m5bnDqYC_q). Instead, use Uri.Segments property and use last path segment (which will be ical.html in this case. Also, don't combine file paths by hand - use Path.Combine:
var uri = new Uri("https://sometest.com/l/admin/ical.html?t=TD61C7NibbV0m5bnDqYC_q");
var lastSegment = uri.Segments[uri.Segments.Length - 1];
string directory = "D:\\Data\\Name";
string filePath = Path.Combine(directory, lastSegment);
WebClient webClient = new WebClient();
webClient.DownloadFile(uri, filePath);
To answer your edited question about getting correct filename. In this case you don't know correct filename until you make a request to server and get a response. Filename will be contained in response Content-Disposition header. So you should do it like this:
var uri = new Uri("https://sometest.com/l/admin/ical.html?t=TD61C7NibbV0m5bnDqYC_q");
string directory = "D:\\Data\\Name";
WebClient webClient = new WebClient();
// make a request to server with `OpenRead`. This will fetch response headers but will not read whole response into memory
using (var stream = webClient.OpenRead(uri)) {
// get and parse Content-Disposition header if any
var cdRaw = webClient.ResponseHeaders["Content-Disposition"];
string filePath;
if (!String.IsNullOrWhiteSpace(cdRaw)) {
filePath = Path.Combine(directory, new System.Net.Mime.ContentDisposition(cdRaw).FileName);
}
else {
// if no such header - fallback to previous way
filePath = Path.Combine(directory, uri.Segments[uri.Segments.Length - 1]);
}
// copy response stream to target file
using (var fs = File.Create(filePath)) {
stream.CopyTo(fs);
}
}

Error while uploading a text file to FTP server C#

I am building a simple application that uploads a .txt file to a FTP server. I have done this before and i am using the same code as i used for the other application.
this is my code:
string localFilePath = #"\\fileprint\data\Groups\Operation\fileExports\dls\";
string archiveFilePath = #"\\fileprint\data\Groups\Operation\fileExports\dls\Archive\";
string logFilePath = #"C:\Users\lmy\Desktop\Logs";
string ftpServer = "ftp://server:21/home/out2233/tmp";
private string logFileName = "" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
public void UploadFile()
{
try
{
string[] files = Directory.GetFiles(localFilePath);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string modified = file.Remove(60, 6);
string modifiedFile = Path.GetFileName(modified);
FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer + modifiedFile));
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpReq.UsePassive = true;
ftpReq.UseBinary = true;
ftpReq.KeepAlive = true;
ftpReq.Credentials = new NetworkCredential("out2233", "password");
ftpReq.EnableSsl = true;
FileInfo fileInfo = new FileInfo(localFilePath + #"\" + fileName);
FileStream fileStream = fileInfo.OpenRead();
byte[] fileContent = new byte[fileInfo.Length];
fileStream.Read(fileContent, 0, Convert.ToInt32(fileInfo.Length));
Stream writer = ftpReq.GetRequestStream();
writer.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
writer.Close();
FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();
AppendLogFile(response, "Uploaded Files: ", fileName);
MoveToArchive(file, archiveFilePath + fileName);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
But it get this error:
exception = {"Cannot send a content-body with this verb-type."}
when the code reaches this line:
Stream writer = ftpReq.GetRequestStream();
I have googled this but i can only find ASP examples. I cant seem to find out what i am doing wrong here. Hope you guys can help me.
thanks!
Looks like, you're trying to list ftp directory content, with this line:
ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
(http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.listdirectorydetails%28v=vs.110%29.aspx)
Try removing it, leaving only this line:
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.uploadfile%28v=vs.110%29.aspx

Get meta data of a file using c#

I need to find a files's meta data using c#.The file i use is saved in third party site.
I can able to download the file from that server but i can't able to get the original meta data of the file that i downloaded.
How to achieve this using c#.Below is my code.
string FilePath = AppDomain.CurrentDomain.BaseDirectory + #"Downloads\";
string Url = txtUrl.Text.Trim();
Uri _Url = new Uri(Url);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(_Url);
request.Timeout = Timeout.Infinite;
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
if (response.ContentType != "text/html; charset=UTF-8")
{
string FileSize = response.Headers.Get("Content-Length");
int lastindex = Url.LastIndexOf("/");
string TempUrlName = Url.Substring(lastindex + 1, Url.Length - (lastindex + 1));
WebClient oWebClient = new WebClient();
oWebClient.DownloadFile(txtUrl.Text.Trim(), FilePath + #"\" + TempUrlName);
if (File.Exists(FilePath + #"\" + TempUrlName))
{
FileInfo oInfo = new FileInfo(FilePath + #"\" + TempUrlName);
DateTime time = oInfo.CreationTime;
time = oInfo.LastAccessTime;
time = oInfo.LastWriteTime;
}
}
I can able to get file size,creation time,last accessed time and last write time only after saving the file in local. But i need the file meta data infos when file is located in server using c#.
Thanks
Since those are properties stored in the file system and changed once you save them locally, you won't be able to access those via HTTP.
Do you have any influence on the third party? Maybe have them send those properties along in the headers?

Categories