设为首页 收藏本站
查看: 873|回复: 0

[经验分享] FTP From PL/SQL

[复制链接]

尚未签到

发表于 2016-6-8 03:47:24 | 显示全部楼层 |阅读模式
    Sometimes it's preferable to trigger FTP jobs directly from PL/SQL rather than rely on CRON or AT. This article contains a brief description of the two methods I use.


  • Shell Script
  • PL/SQL FTP API
  • ACL for 11g

Shell Script
  The first method relies on a java stored procedure, described in Shell Commands From PL/SQL, which can be used to trigger a shell script to perform the transfer. The shell script may look like the following.


#! /bin/ksh
# Move to appropriate directory on local server
cd /extracts
# FTP all files in directory
ftp -inv ftp.company.com <<EOF
user ftpuser ftppassword
# Move to appropriate directory on remote server.
cd /loads
ascii
mput *.*
bye
EOF

PL/SQL FTP API
  The second approach uses a combination of the UTL_TCP and UTL_FILE packages to create a simple FTP API (ftp.pks, ftp.pkb). Once the API is loaded into the appropriate schema simple FTP commands can be initiated as follows.


CREATE OR REPLACE DIRECTORY my_docs AS '/u01/app/oracle/';
SET SERVEROUTPUT ON SIZE 1000000
@c:\ftp.pks
@c:\ftp.pkb
-- Retrieve an ASCII file from a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.ascii(p_conn => l_conn);
ftp.get(p_conn      => l_conn,
p_from_file => '/u01/app/oracle/test.txt',
p_to_dir    => 'MY_DOCS',
p_to_file   => 'test_get.txt');
ftp.logout(l_conn);
END;
/
-- Send an ASCII file to a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.ascii(p_conn => l_conn);
ftp.put(p_conn      => l_conn,
p_from_dir  => 'MY_DOCS',
p_from_file => 'test_get.txt',
p_to_file   => '/u01/app/oracle/test_put.txt');
ftp.logout(l_conn);
END;
/
-- Retrieve a binary file from a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.binary(p_conn => l_conn);
ftp.get(p_conn      => l_conn,
p_from_file => '/u01/app/oracle/product/9.2.0.1.0/sysman/reporting/gif/jobs.gif',
p_to_dir    => 'MY_DOCS',
p_to_file   => 'jobs_get.gif');
ftp.logout(l_conn);
END;
/
-- Send a binary file to a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.binary(p_conn => l_conn);
ftp.put(p_conn      => l_conn,
p_from_dir  => 'MY_DOCS',
p_from_file => 'jobs_get.gif',
p_to_file   => '/u01/app/oracle/jobs_put.gif');
ftp.logout(l_conn);
END;
/
-- Get a directory listing from a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
l_list  ftp.t_string_table;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.list(p_conn   => l_conn,
p_dir   => '/u01/app/oracle',
p_list  => l_list);
ftp.logout(l_conn);
IF l_list.COUNT > 0 THEN
FOR i IN l_list.first .. l_list.last LOOP
DBMS_OUTPUT.put_line(i || ': ' || l_list(i));
END LOOP;
END IF;
END;
/
-- Get a directory listing (file names only) from a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
l_list  ftp.t_string_table;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.nlst(p_conn   => l_conn,
p_dir   => '/u01/app/oracle',
p_list  => l_list);
ftp.logout(l_conn);
IF l_list.COUNT > 0 THEN
FOR i IN l_list.first .. l_list.last LOOP
DBMS_OUTPUT.put_line(i || ': ' || l_list(i));
END LOOP;
END IF;
END;
/
-- Rename a file on a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.rename(p_conn => l_conn,
p_from => '/u01/app/oracle/dba/shutdown',
p_to   => '/u01/app/oracle/dba/shutdown.old');
ftp.logout(l_conn);
END;
/
-- Delete a file on a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.delete(p_conn => l_conn,
p_file => '/u01/app/oracle/dba/temp.txt');
ftp.logout(l_conn);
END;
/
-- Create a directory on a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.mkdir(p_conn => l_conn,
p_dir => '/u01/app/oracle/test');
ftp.logout(l_conn);
END;
/
-- Remove a directory from a remote FTP server.
DECLARE
l_conn  UTL_TCP.connection;
BEGIN
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.rmdir(p_conn => l_conn,
p_dir  => '/u01/app/oracle/test');
ftp.logout(l_conn);
END;
/
  The basic functions are implemented using LOBs to allow FTP without having to access files on the local filesystem. The get and put procedures string these together to form a complete job using all the functions. If a straight forward FTP to, or from, the local filesystem is required it is more efficient to use theGET_DIRECT and PUT_DIRECT procedures as they avoid the temporary LOBs.
  The current implementation has the following issues:


  • The mput and mget operations are not supported directly, but can be implemented using a combination of the list/nlst and get/put operations.
  • The implementation of binary transfers relies on UTL_FILE features only available in Oracle9i Release 2 upwards.
  • There is no support for ASCII mode in the PUT_DIRECT procedure.
  Thanks to Hans van Doormalen for noticing I wasn't closing my passive connections. I do now :)

ACL for 11g
  The introduction of Fine-Grained Access to Network Services in Oracle Database 11g Release 1 means you will need to configure an access control list (ACL) to allow UTL_TCP to access the network. The examples above work correctly with the following basic ACL. You will need to amend the FTP server details and username details to match your FTP server address and the Oracle username running the FTP API.


DECLARE
l_acl_name         VARCHAR2(30) := 'utl_tcp.xml';
l_ftp_server_ip    VARCHAR2(20) := '192.168.0.131';
l_ftp_server_name  VARCHAR2(20) := 'ftp.company.com';
l_username         VARCHAR2(30) := 'TEST';
BEGIN
DBMS_NETWORK_ACL_ADMIN.create_acl (
acl          => l_acl_name,
description  => 'Allow connections using UTL_TCP',
principal    => l_username,
is_grant     => TRUE,
privilege    => 'connect',
start_date   => SYSTIMESTAMP,
end_date     => NULL);
COMMIT;
DBMS_NETWORK_ACL_ADMIN.add_privilege (
acl         => l_acl_name,
principal   => l_username,
is_grant    => FALSE,
privilege   => 'connect',
position    => NULL,
start_date  => NULL,
end_date    => NULL);
COMMIT;
DBMS_NETWORK_ACL_ADMIN.assign_acl (
acl         => l_acl_name,
host        => l_ftp_server_ip,
lower_port  => NULL,
upper_port  => NULL);
DBMS_NETWORK_ACL_ADMIN.assign_acl (
acl         => l_acl_name,
host        => l_ftp_server_name,
lower_port  => NULL,
upper_port  => NULL);
COMMIT;
END;
/


  
  http://www.oracle-base.com/articles/misc/ftp-from-plsql.php

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227498-1-1.html 上篇帖子: ftp读取文件 下篇帖子: ftp 中文路径乱码问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表